Prerequisite: String in C#
StringBuilder is used to represent a mutable string of characters. Mutable means the string which can be changed. So String objects are immutable but StringBuilder is the mutable string type. It will not create a new modified instance of the current string object but do the modifications in the existing string object. The complete functionality of StringBuilder is provided by StringBuilder class which is present in
Need of the StringBuilder: As stated above that the String class objects are immutable which means that if the user will modify any string object it will result into the creation of a new string object. It makes the use of string costly. So when the user needs the repetitive operations on the string then the need of StringBuilder come into existence. It provides the optimized way to deal with the repetitive and multiple string manipulation operations.
Using String Class: Geeks Using StringBuilder Class: GeeksforGeeks
- Use of concat1 Method: In this method, we are passing a string “Geeks” and performing “s1 = String.Concat(s1, st);” where st is “forGeeks” to be concatenated. The string passed from Main() is not changed, this is due to the fact that String is immutable. Altering the value of string creates another object and s1 in concat1() stores reference of the new string. But the references s1 in Main() and concat1() refer to different strings.
- Use of concat2 Method: In this method, we are passing a string “Geeks” and performing “s2.Append(“forGeeks”)” which changes the actual value of the string (in Main) to “GeeksforGeeks”. This is due to the simple fact that StringBuilder is mutable and hence changes its value.
When to use which one:
- If a string is going to remain constant throughout the program, then use String class object because a String object is immutable.
- If a string can change (example: lots of logic and operations in the construction of the string) then using a StringBuilder is the best option.
Converting String to StringBuilder:
To convert a String class object to StringBuilder class object, just pass the string object to the StringBuilder class constructor.
Converting StringBuilder to String:
This conversions can be performed using ToString() method.
StringBuilder object to String: Builder
- StringBuilder in C#
- How to create the StringBuilder in C#
- How to find the length of the StringBuilder in C#
- How to remove all characters from StringBuilder in C#
- StringBuilder.EnsureCapacity() Method in C#
- C# | Creating StringBuilder having specified capacity
- How to find the MaxCapacity of a StringBuilder in C#
- StringBuilder.CopyTo Method in C#
- StringBuilder.Chars Property in C#
- How to find the Capacity of a StringBuilder in C#
- StringBuilder.ToString Method in C#
- C# | Check if two StringBuilder objects are Equal
- File.Replace(String, String, String, Boolean) Method in C# with Examples
- File.Replace(String, String, String) Method in C# with Examples
- File.WriteAllLines(String, IEnumerable<String>, Encoding) Method in C# with Examples
- File.AppendAllLines(String, IEnumerable<String>, Encoding) Method in C# with Examples
- File.AppendAllText(String, String, Encoding) Method in C# with Examples
- File.Copy(String, String, Boolean) Method in C# with Examples
- File.WriteAllLines(String, IEnumerable<String>) Method in C# with Examples
- File.WriteAllText(String, String, Encoding) Method in C# with Examples
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.