Often we do large string concatenation in our code without giving a thought to performance. Consider following example of string concatenation.
string s1 = "orange";
string s2 = "red";
s1 += s2;
System.Console.WriteLine(s1); // outputs "orangered"
s1 = s1.Substring(2, 5);
System.Console.WriteLine(s1); // outputs "anger"
String objects are immutable, meaning that they cannot be changed once they have been created. Methods that act on strings actually return new string objects. In the previous example, when the contents of s1 and s2 are concatenated to form a single string, the two strings containing "orange" and "red" are both unmodified. The += operator creates a new string that contains the combined contents. The result is that s1 now refers to a different string altogether. A string containing just "orange" still exists, but is no longer referenced when s1 is concatenated.
Therefore, for performance reasons, large amounts of concatenation or other involved string manipulation should be performed with the StringBuilder class, like this:
System.Text.StringBuilder sb = new System.Text.StringBuilder();
sb.Append("one ");
sb.Append("two ");
sb.Append("three");
string str = sb.ToString();
The StringBuilder class creates a string buffer that offers better performance if your program performs a lot of string manipulation. The StringBuilder string also allows you to reassign individual characters, something the built-in string data type does not support. This code, for example, changes the content of a string without creating a new string:
System.Text.StringBuilder sb = new System.Text.StringBuilder("Rat: the ideal pet");
sb[0] = 'C';
System.Console.WriteLine(sb.ToString());
System.Console.ReadLine();
class TestStringBuilder
{
static void Main()
{
System.Text.StringBuilder sb = new System.Text.StringBuilder();
// Create a string composed of numbers 0 - 9
for (int i = 0; i <>
{
sb.Append(i.ToString());
}
System.Console.WriteLine(sb); // displays 0123456789
string s1 = "orange";
string s2 = "red";
s1 += s2;
System.Console.WriteLine(s1); // outputs "orangered"
s1 = s1.Substring(2, 5);
System.Console.WriteLine(s1); // outputs "anger"
String objects are immutable, meaning that they cannot be changed once they have been created. Methods that act on strings actually return new string objects. In the previous example, when the contents of s1 and s2 are concatenated to form a single string, the two strings containing "orange" and "red" are both unmodified. The += operator creates a new string that contains the combined contents. The result is that s1 now refers to a different string altogether. A string containing just "orange" still exists, but is no longer referenced when s1 is concatenated.
Therefore, for performance reasons, large amounts of concatenation or other involved string manipulation should be performed with the StringBuilder class, like this:
System.Text.StringBuilder sb = new System.Text.StringBuilder();
sb.Append("one ");
sb.Append("two ");
sb.Append("three");
string str = sb.ToString();
The StringBuilder class creates a string buffer that offers better performance if your program performs a lot of string manipulation. The StringBuilder string also allows you to reassign individual characters, something the built-in string data type does not support. This code, for example, changes the content of a string without creating a new string:
System.Text.StringBuilder sb = new System.Text.StringBuilder("Rat: the ideal pet");
sb[0] = 'C';
System.Console.WriteLine(sb.ToString());
System.Console.ReadLine();
class TestStringBuilder
{
static void Main()
{
System.Text.StringBuilder sb = new System.Text.StringBuilder();
// Create a string composed of numbers 0 - 9
for (int i = 0; i <>
{
sb.Append(i.ToString());
}
System.Console.WriteLine(sb); // displays 0123456789
Comments