For more efficient string manipulation, you can use the StringBuilder class, which has methods that allow you to modify its internal character data without allocating a new string for each operation.
A StringBuilder instance wraps a single Unicode string and allows you to modify that string in different ways.
StringBuilder can be found in the System.Text namespace.
Constructing a StringBuilder:
1
2
| StringBuilder sb1 = new StringBuilder(); // Empty stringStringBuilder sb2 = new StringBuilder("Sean"); |
Modifying internal string:
1
2
3
4
| sb2.Append(" was here");sb2.AppendFormat(" on {0:d}", DateTime.Today);sb2.Replace("Sean", "Kilroy");sb2.Insert(0, "Mr. "); // Insert at start of string |
Other things that you can do with a StringBuilder object:
1
2
3
| char third = sb2[2]; // 3rd characterstring s = sb2.ToString(); // Convert to stringint len = sb2.Length; // # chars |

