StringBuilder
which is more efficient because it does contain a mutable string buffer. .NET String are immutable which is the reason why a new string object is created every time we alter it (insert, append, remove, etc.).
The String object is immutable. Every time you use one of the methods in the System.String class, you create a new string object in memory, which requires a new allocation of space for that new object. In situations where you need to perform repeated modifications to a string, the overhead associated with creating a new String object can be costly. The System.Text..::.StringBuilder class can be used when you want to modify a string without creating a new object. For example, using the StringBuilder class can boost performance when concatenating many strings together in a loop.
Although the StringBuilder is a dynamic object that allows you to expand the number of characters in the string that it encapsulates, you can specify a value for the maximum number of characters that it can hold. This value is called the capacity of the object and should not be confused with the length of the string that the current StringBuilder holds. For example, you might create a new instance of the StringBuilder class with the string "Hello", which has a length of 5, and you might specify that the object has a maximum capacity of 25. When you modify the StringBuilder, it does not reallocate size for itself until the capacity is reached. When this occurs, the new space is allocated automatically and the capacity is doubled. You can specify the capacity of the StringBuilder class using one of the overloaded constructors. The following example specifies that the MyStringBuilder object can be expanded to a maximum of 25 spaces.
StringBuilder MyStringBuilder = new StringBuilder("Hello World!", 25); |
The Append method can be used to add text or a string representation of an object to the end of a string represented by the current StringBuilder. The following example initializes a StringBuilder to "Hello World" and then appends some text to the end of the object. Space is allocated automatically as needed.
StringBuilder MyStringBuilder = new StringBuilder("Hello World!"); MyStringBuilder.Append(" What a beautiful day."); Console.WriteLine(MyStringBuilder); |
The AppendFormat method adds text to the end of the StringBuilder, but also implements the IFormattable interface and therefore accepts the standard format strings described in the formatting section. You can use this method to customize the format of variables and append those values to a StringBuilder. The following example uses the AppendFormat method to place an integer value formatted as a currency value at the end of a StringBuilder.
int MyInt = 25; StringBuilder MyStringBuilder = new StringBuilder("Your total is "); MyStringBuilder.AppendFormat("{0:C} ", MyInt); Console.WriteLine(MyStringBuilder); |
This example displays Your total is $25.00 to the console.
StringBuilder.ToString Method
Converts the value of this instance to a String.
So, I strongly suggest to use the .NET StringBuilder to concatenate strings instead of the concatenation operator ("+").