The string object cannot be changed. Every time you use a method in the system. string class, you must create a new String object in the memory, which requires a new space for the new object. If you need to modify the string repeatedly, the system overhead associated with creating a new String object may be very expensive.
If you want to modify the string without creating a new object, you can use the system. Text. stringbuilder class. For example, when many strings are connected together in a loop, using the stringbuilder class can improve performance. Stringbuilder is 200 times faster than string.
By initializing variables using an overloaded constructor method, you can create a new instance of the stringbuilder class, as described in the following example.
Stringbuilder mystringbuilder = new stringbuilder ("Hello world! ");
Set the capacity and length of the stringbuilder object
The stringbuilder object is a dynamic object that allows you to expand the number of characters in its encapsulated strings. You can specify a value for the maximum number of characters it can accommodate.
Stringbuilder mystringbuilder = new stringbuilder ("Hello world! ", 25 );
In addition, you can use the read/write capacity attribute to set the maximum length of an object. The following code uses the capacity attribute to define the maximum length of an object.
Mystringbuilder. capacity = 25;
(2) The following describes several common methods of this type:
(1) The append method can be used to add the string representation of a text or object to the end of a string represented by the current stringbuilder object. In the following example, A stringbuilder object is initialized as "Hello World" and some text is appended to the end of the object. Space will be automatically allocated as needed.
Stringbuilder mystringbuilder = new stringbuilder ("Hello world! ");
Mystringbuilder. append ("What a beautiful day .");
Console. writeline (mystringbuilder );
In this example, hello World! What a beautiful day. displayed on the console.
(2) The appendformat method adds text to the end of stringbuilder and implements the iformattable interface. Therefore, the standard format string described in the formatting section is acceptable. You can use this method to customize the variable format and append these values to the end of stringbuilder. The following example uses the appendformat method to place an integer 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 );
In this example, the your total is $25.00 is displayed on 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 to the sixth position of stringbuilder.
Stringbuilder mystringbuilder = new stringbuilder ("Hello world! ");
Mystringbuilder. insert (6, "beautiful ");
Console. writeline (mystringbuilder );
In this example, hello beautiful world! Displayed on the console.
(4) You can use the remove method to remove a specified number of characters from the current stringbuilder. The removal process starts from the specified index starting from scratch. The following example uses the Remove Method to shorten stringbuilder.
Stringbuilder mystringbuilder = new stringbuilder ("Hello world! ");
Mystringbuilder. Remove (5, 7 );
Console. writeline (mystringbuilder );
In this example, hello is displayed on the console.
(5) using the replace method, you can use another specified character to replace the characters in the stringbuilder object. The following example uses the replace method to search for the stringbuilder object and find all exclamation point characters (!), Use the question mark character (?) To replace them.
Stringbuilder mystringbuilder = new stringbuilder ("Hello world! ");
Mystringbuilder. Replace ('! ','? ');