The StringBuilder object is dynamic and can change the contents of a string. StringBuilder method to modify the contents of a string.
1:stringbuilder.append (). Appends the content to the end of the current StringBuilder string.
StringBuilder MYSB = new StringBuilder ("Hello!"); Mysb.append ("Hello everyone!") "); Console.WriteLine (MYSB);
2:stringbuilder.appendformat (). Use a formatted text instead of a string to pass the symbol.
int Mymoney = 24; StringBuilder MYSB = new StringBuilder ("The money owed to me is"); Mysb.appendformat ("{0:c}", Mymoney); Console.WriteLine (MYSB);
3:stringbuilder.insert (int,string). Inserts a string and an object into the current StringBuilder string at the specified location.
4:stringbuilder.remove (P,n). Removes the specified character from the current StringBuilder object, meaning that n characters are removed from the P position.
5:stringbuilder.replace (A, B). is to replace A with B for the specified string, and A and B are both characters or strings.
String A= "AAA AAA AAA";
String B=a.replace (' A ', ' w ');
Results output "www www www".
The above methods are also used in string, even more than StringBuilder. The more out is as follows: PadRight ()/padleft () method, according to the specified width, with the corresponding number of the given word Fu Tiancai character, if no padding character is specified, fill with white space.
String a= "ABCDEFG";
A=a.padright (a.length+3, "!");
The result outputs "ABCDEFG!!!".
Split (char[]) method, the char array contains separators that can be used to split strings into substrings that are returned as elements in an array of strings.
The Substring () method extracts the specified length string starting at the specified position n and, if specified as 1, extracts a substring of length 1.
ToUpper ()/tolower () that returns a copy of the case of the string.
Trim ()/trim (char[]), removes all whitespace characters from the string, and if provided is a char array, deletes the leading and trailing characters specified by this char array.
TrimEnd (char[])/trimstart ([]), removes all leading and trailing characters specified by the char number, and removes the white space character if the array is null.
ToCharArray ()/tochararray (n,1), extracts the character from the string and puts it in a Unicode character array
The String object is immutable. Each time you use one of the methods in the System.String class, you create a new string object in memory, which requires a new space to be allocated for the new object. The overhead associated with creating a new string object can be very expensive in situations where you need to perform repeated modifications to the string.
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 you concatenate many strings together in a loop.