Console.WriteLine('{0:#,0}', num3); //---9,876---
num3 = 1239876;
Console.WriteLine('{0:#,0}', num3); //---1,239,876---
Last, to format a special number (such as a phone number), use the following format specifier:
long phoneNumber = 1234567890;
Console.WriteLine('{0:###-###-####}', phoneNumber); //---123-456-7890---
The StringBuilder Class
Earlier in this chapter you saw how to easily concatenate two strings by using the +
operator. That's fine if you are concatenating a small number of strings, but it is not recommended for large numbers of strings. The reason is that String
objects in .NET are
One important application of the StringBuilder
class is its use in .NET interop with native C/C++ APIs that take string arguments and modify strings. One example of this is the Windows API function GetWindowText()
. This function has a second argument that takes a TCHAR*
parameter. To use this function from .NET code, you would need to pass a StringBuilder
object as this argument.
Consider the following example, where you concatenate all the numbers from 0 to 9999:
int counter = 9999;
string s = string.Empty;
for (int i = 0; i <= counter; i++) {
s += i.ToString();
}
Console.WriteLine(s);
At first glance, the code looks innocent enough. But let's use the Stopwatch
object to time the operation. Modify the code as shown here:
string s = string.Empty;
for (int i = 0; i <= counter; i++) {
s += i.ToString();
}
Console.WriteLine(s);
On average, it took about 374 ms on my computer to run this operation. Let's now use the StringBuilder
class in .NET to perform the string concatenation, using its Append()
method:
System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch();
sw.Start();
sw.Stop();
Console.WriteLine('Took {0} ms', sw.ElapsedMilliseconds);
On average, it took about 6 ms on my computer to perform this operation. As you can deduce, the improvement is drastic — 98% ((374-6)/374). If you increase the value of the loop variant (counter), you will find that the improvement is even more dramatic.
The StringBuilder
class represents a mutable string of characters. Its behavior is like the String
object except that its value can be modified once it has been created.
The StringBuilder
class contains some other important methods, which are described in the following table.
Method | Description |
---|---|
Append | Appends the string representation of a specified object to the end of this instance. |
AppendFormat | Appends a formatted string, which contains zero or more format specifiers, to this instance. Each format specification is replaced by the string representation of a corresponding object argument. |
AppendLine | Appends the default line terminator, or a copy of a specified string and the default line terminator, to the end of this instance. |
CopyTo | Copies the characters from a specified segment of this instance to a specified segment of a destination Char array. |
Insert | Inserts the string representation of a specified object into this instance at a specified character position. |
Remove | Removes the specified range of characters from this instance. |
Replace |