SoFunction
Updated on 2025-03-06

Summary of the usage of StringBuilder class in C#

A String object is unchangeable. Each time one of the methods in the class is used, a new string object is created in memory, which requires allocating new space for the new object. The system overhead associated with creating a new String object can be very expensive in situations where repeated modifications are required to be performed on the string. If you want to modify a string without creating a new object, you can use the class. For example, using the StringBuilder class can improve performance when concatenating many strings together in a loop.

By initializing the variable with an overloaded constructor method, a new instance of the StringBuilder class can be created, as explained in the following example.

StringBuilder MyStringBuilder = new StringBuilder("Hello World!");

(I) Set capacity and length
Although a StringBuilder object is a dynamic object that allows the number of characters in the string it encapsulates, you can specify a value for the maximum number of characters it can hold. This value is called the capacity of the object and should not be confused with the string length that the current StringBuilder object holds. For example, you can create a new instance of the StringBuilder class with the string "Hello" (length 5), and you can specify that the maximum capacity of the object is 25. When a StringBuilder is modified, it does not reallocate space for itself until the capacity is reached. When capacity is reached, new space will be automatically allocated and the capacity will be doubled. You can use one of the overloaded constructors to specify the capacity of the StringBuilder class. The following code example specifies that the MyStringBuilder object can be expanded to a maximum of 25 blanks.
StringBuilder MyStringBuilder = new StringBuilder("Hello World!", 25);
Additionally, you can use the Read/Write Capacity property to set the maximum length of the object. The following code example uses the Capacity property to define the maximum length of an object.
= 25;

(II) The following lists several common methods of this type:
(1)Append
Methods can be used to add a string representation of a text or object to the end of a string represented by the current StringBuilder object. The following example initializes a StringBuilder object to "Hello World" and then appends some text to the end of the object. Space will be automatically allocated as needed.
StringBuilder MyStringBuilder = new StringBuilder("Hello World!");
(" What a beautiful day.");
(MyStringBuilder);
This example displays Hello World! What a beautiful day. to the console.

(2)AppendFormatThe method adds text to the end of the StringBuilder and implements the IFormattable interface, so it accepts the standard format strings described in the Formatted section. You can use this method to customize the format of the variable and append these values ​​to the aftermath of the StringBuilder. The following example uses the AppendFormat method to place an integer value set to currency value format at the end of StringBuilder.
int MyInt = 25;
StringBuilder MyStringBuilder = new StringBuilder("Your total is ");
("{0:C} ", MyInt);
(MyStringBuilder);
This example displays Your total is $25.00 to the console.

(3)InsertMethod adds a string or object to the specified location in the current StringBuilder. The following example uses this method to insert a word into the sixth position of the StringBuilder.
StringBuilder MyStringBuilder = new StringBuilder("Hello World!");
(6,"Beautiful ");
(MyStringBuilder);
This example displays Hello Beautiful World! to the console.

(4) You can use RemoveThe method removes the specified number of characters from the current StringBuilder, and the removal process starts at the specified zero-based index. The following example uses the Remove method to shorten the StringBuilder.
StringBuilder MyStringBuilder = new StringBuilder("Hello World!");
(5,7);
(MyStringBuilder);
This example displays Hello to the console.

(5) Use ReplaceMethod, you can replace characters in the StringBuilder object with another specified character. The following example uses the Replace method to search for StringBuilder objects, find all exclamation mark characters (!), and replace them with question mark characters (?).
StringBuilder MyStringBuilder = new StringBuilder("Hello World!");
('!', '?');
(MyStringBuilder);
This example displays Hello World? to the console