1) string is a reference type and cannot be modified once the string is created
For example:
String a= "AAA";
String B=a;
A is the same as the B value, but not the same object;
Each time you use a method in the System.String class, you create a new string object in memory and then allocate a new space for the new object. The system memory loss becomes larger when the string modification is more frequent.
2) StringBuilder is a variable sequence of characters that can be modified by an object of type StringBuilder, which points to an array of char structures, using StringBuilder to improve performance.
3) Common methods of StringBuilder
1:append (): Adds a string representation of text or an object to the end of the current StringBuilder object's string
StringBuilder sb = new StringBuilder ("I want to go shopping!") ");
Sb. Append ("The sun is good today,");
Console.WriteLine (SB);
2:appendformat (): Implements the IFormattable interface, which accepts a standard format string described in the Format section, using this method to customize the variable and append the value to the StringBuilder
int MyInt = 10;
StringBuilder Mystringbuilder = new StringBuilder ("The money is");
Mystringbuilder.appendformat ("{0:c}", MyInt);
Console.WriteLine (Mystringbuilder);
The money is $25.00;
3:remove (): You can use the Remove method to remove a specified number of characters from the current StringBuilder, and the removal process starts at the specified zero-based index.
StringBuilder Mystringbuilder = new StringBuilder ("Hello world!");
Mystringbuilder.remove (5,7);
Console.WriteLine (Mystringbuilder);
4:replace (): Using the Replace method, you can replace the characters within the StringBuilder object with another specified character.
StringBuilder Mystringbuilder = new StringBuilder ("Hello world!");
Mystringbuilder.replace ('! ', '? ');
Console.WriteLine (Mystringbuilder)
5:insert (): The Insert method adds a string or object to the specified position in the current StringBuilder.
StringBuilder Mystringbuilder = new StringBuilder ("Hello world!");
Mystringbuilder.insert (6, "Beautiful");
Console.WriteLine (Mystringbuilder)
String and StringBuilder