Summary of the use of StringBuilder in C

  • 2020-06-07 05:11:56
  • OfStack

The String object is immutable. Every time you use method 1 in the System. String class, a new string object is created in memory, which requires new space to be allocated for the new object. The overhead associated with creating a new String object can be very expensive in cases where repeated changes to the string are required. If you want to modify a string without creating a new object, you can use the System.Text.StringBuilder class. For example, using the StringBuilder class can improve performance when concatenating many strings from 1 to 1 in a loop.

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

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

(1) Setting capacity and length
While the StringBuilder object is dynamic and allows you to expand the number of characters in the string it encapsulates, you can specify a value of 1 for the maximum number of characters it can hold. This value is called the size of the object and should not be confused with the length of the string held by the current StringBuilder object at 1. For example, you can create a new instance of the StringBuilder class with the string "Hello" (length 5), and you can specify the maximum size of the object to be 25. When modifying StringBuilder, it does not reallocate space for itself until the capacity is reached. When the capacity is reached, new space is automatically allocated and the capacity is doubled. The overloaded constructor 1 can be used 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 white Spaces.
StringBuilder MyStringBuilder = new StringBuilder("Hello World!", 25);
In addition, you can use the read/write Capacity attribute to set the maximum length of the object. The following code example USES the Capacity attribute to define the maximum length of an object.
MyStringBuilder.Capacity = 25;

(2) Several common methods of this class are listed below:
(1) The Append method can be used to add a string representation of text or an object to the end of the string represented by the current StringBuilder object. The following example initializes an StringBuilder object as "Hello World" and appends some text to the end of the object. Space is automatically allocated as needed.
StringBuilder MyStringBuilder = new StringBuilder("Hello World!");
MyStringBuilder.Append(" What a beautiful day.");
Console.WriteLine(MyStringBuilder);
This example will Hello World! What a beautiful day. Display to the console.

(2) The AppendFormat method adds text to the end of StringBuilder and implements the IFormattable interface, thus accepting the standard format string described in the formatting section. You can use this method to customize the format of the variables and append the values to StringBuilder. The following example USES the AppendFormat method to place an integer value set to the currency value format at the end of 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.

(3) The Insert method adds a string or object to the specified position in the current StringBuilder. The following example USES this method to insert a word into position 6 of StringBuilder.
StringBuilder MyStringBuilder = new StringBuilder("Hello World!");
MyStringBuilder.Insert(6,"Beautiful ");
Console.WriteLine(MyStringBuilder);
This example will Hello Beautiful World! Display to the console.

(4) The Remove method can be used to remove a specified number of characters from the current StringBuilder, starting at the specified zero-based index. The following example USES the Remove method to shorten StringBuilder.
StringBuilder MyStringBuilder = new StringBuilder("Hello World!");
MyStringBuilder.Remove(5,7);
Console.WriteLine(MyStringBuilder);
This example displays Hello to the console.

(5) Using the Replace method, the character in the StringBuilder object can be replaced with another specified character. The following example USES the Replace method to search the StringBuilder object for all exclamation mark characters (!) , and the question mark character (?) Let's replace them.
StringBuilder MyStringBuilder = new StringBuilder("Hello World!");
MyStringBuilder.Replace('!', '?');
Console.WriteLine(MyStringBuilder);
This example puts Hello World? Display to the console


Related articles: