The String object cannot be changed. One of the methods in the System. String class is to create a new String object in the memory, which requires a new space for the 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.
<1> set the capacity and length
Although the StringBuilder object is a dynamic object that allows you to expand the number of characters in its closed string, you can specify a value for its maximum number of characters. The reference value is the capacity of this object and should not be confused with the string length of the current StringBuilder object. For example, you can create a new instance of the StringBuilder class with the string "Hello" (Length: 5) and specify the maximum capacity of this object to 25. When the capacity is reached, a new space is automatically allocated and the capacity is doubled. You can use one of the overloaded constructors to specify the capacity of the StringBuilder class.
In the following example, the MyStringBuilder object can be expanded to a maximum of 25 spaces.
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 routine uses the Capacity attribute to define the maximum length of an object.
MyStringBuilder. Capacity = 25;
The EnsureCapacity method can be used to check the current capacity of the StringBuilder class. If the capacity is greater than the passed value, no changes are made. However, if the capacity is smaller than the passed value, the side changes the current capacity to match the passed value.
You can also view or set the length attribute. If you set the length attribute to a value greater than the Capacity attribute, the capacity attribute is automatically changed to the same value as the Length attribute. Setting the Length attribute to a value smaller than the string Length in the current StringBuilder object will shorten the string.
<2> modify the StringBuilder string
(1) Append: This method can be used to add the string representation of the text or object to the end of the string represented by the current StringBuilder object.
In the following example, the StringBuilder object is initialized to "Hello World", and some text is traced back to the end of the StringBuilder object. Space will be automatically allocated as needed.
StringBuilder MyStringBuilder = new StringBuilder ("Hello World ");
MyStringBuilder. Append ("What a beautiful day ");
Console. WriteLine (MyStringBuilder );
Program running result: Hello World! What a beautiful day
(2) AppendFormat: This method adds text to the end of the StringBuilder object 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 the StringBuilder object.
The following uses the AppendFormat method to rotate an integer formatted as a currency to the end of the StringBuilder class.
Int MyInt = 25;
StringBuilder MyStringBuilder = new StringBuilder ("Total Amount :");
MyStringBuilder. AppendFormat ("{0: C}", MyInt );
Console. WriteLine (MyStringBuilder );
Program running result: ¥25.00.
(3) Insert: This method adds a string or object to a specified position in the current StringBuilder object.
The following example uses this method to insert a word to the 3rd position of the StringBuilder class.
StringBuilder MyStringBuilder = new StringBuilder ("Hello, world! ");
MyStringBuilder. Insert (3, "beautiful ");
Console. WriteLine (MyStringBuilder );
Program running result: Hello, beautiful world!
(4) Remove: You can use this method to Remove a specified number of characters from the current StringBuilder object. The removal process starts from the specified/index starting from scratch.
The following example uses the Remove Method to shorten the StringBuilder object.
StringBuilder MyStringBuilder = new StringBuilder ("Hello, world! ");
MyStringBuilder. Remove (2, 4 );
Console. WriteLine (MyStringBuilder );
Program running result: Hello
(5) Replace: 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 to find the exclamation point character (!) And use the question mark (?) To replace them.
StringBuilder MyStringBuilder = new StringBuilder ("Hello, world! ");
MyStringBuilder. Replace (!, ?);
Console. WriteLine (MyStringBuilder );
Program running result: Hello, world?
<3> character encoding
A character is an abstract entity that can be expressed using multiple character schemes or code pages. For example, Unicode UTF-16 encoding represents a 16-bit integer sequence, while Unicode UTF-8 encoding represents the same character as an 8-bit sequence. The Common Language Runtime uses Unicode UTF-16 (Unicode conversion format, 16-bit encoding form) to represent characters.
The application uses encoding to map character expressions from the local character scheme to other schemes. Applications can also use decoding to map character expressions from non-local schemes to local schemes. The following table lists the most common classes used for character encoding and decoding in the System. Text namespace.
Description of the character Scheme
ASCII code ystem. Text. ASCIIEncoding converts the characters and ASCII characters
Multiple encodings: System. Text. Encoding converts the characters to the various encodings specified in the Convert method.
UTF-16 Unicode encoding System. Text. UnicodeEncoding converts between other encodings and UTF-16 encodings. In this solution, the characters are expressed as 16-digit integers.
UTF-8 Unicode encoding System. Text. UTF8Encoding converts between other encodings and UTF-8 encodings. This width allows the encoding scheme to use 1 ~ Four bytes indicate characters.