Open In App

C# String vs StringBuilder

Last Updated : 11 Jan, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

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 System.Text namespace.

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.

String vs StringBuilder

FeatureStringStringBuilder
MutabilityImmutable (cannot be changed after creation)Mutable (can be changed without creating new objects)
PerformanceSlower for frequent modificationsFaster for frequent modifications
Memory UsageCreates a new object for each modificationModifies the same object, reducing memory overhead
Use CaseUse for small or infrequently modified stringsUse for large or frequently modified strings
Modification MethodsModification requires creating a new stringModification is done in-place
Thread SafetyStrings are thread-safeStringBuilder is not inherently thread-safe

Example: Demonstrating the differences between String and StringBuilder

C#
// Difference between String Vs StringBuilder usingSystem;usingSystem.Text;usingSystem.Collections;classGeeks{// Concatenates to String publicstaticvoidconcat1(Strings1){// taking a string which // is to be Concatenate Stringst="forGeeks";// using String.Concat method // you can also replace it with // s1 = s1 + "forgeeks"; s1=String.Concat(s1,st);}// Concatenates to StringBuilder publicstaticvoidconcat2(StringBuilders2){// using Append method // of StringBuilder class s2.Append("forGeeks");}// Main Method publicstaticvoidMain(String[]args){Strings1="Geeks";concat1(s1);// s1 is not changed Console.WriteLine("Using String Class: "+s1);StringBuilders2=newStringBuilder("Geeks");concat2(s2);// s2 is changed Console.WriteLine("Using StringBuilder Class: "+s2);}}

Output
Using String Class: Geeks Using StringBuilder Class: GeeksforGeeks 

Explanation:

  • 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.

Converting String to StringBuilder

To convert a String class object to StringBuilder class object, just pass the string object to the StringBuilder class constructor.

Example:

C#
// Conversion from String to StringBuilder. usingSystem;usingSystem.Text;classGeeks{// Main Method publicstaticvoidMain(String[]args){Stringstr="Geeks";// conversion from String object // to StringBuilder StringBuildersbl=newStringBuilder(str);sbl.Append("ForGeeks");Console.WriteLine(sbl);}}

Output
GeeksForGeeks 

Converting StringBuilder to String

This conversions can be performed using ToString() method.

Example:

C#
// Conversion from String to StringBuilder usingSystem;usingSystem.Text;classGeeks{// Main Method publicstaticvoidMain(String[]args){StringBuildersbdr=newStringBuilder("Builder");// conversion from StringBuilder // object to String using ToString method Stringstr1=sbdr.ToString();Console.Write("StringBuilder object to String: ");Console.WriteLine(str1);}}

Output
StringBuilder object to String: Builder 




Next Article
Article Tags :

Similar Reads

close