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 performs modifications on the same StringBuilder object without creating a new object.
The complete functionality of StringBuilder is provided by StringBuilder class which is present in System.Text namespace.
String vs StringBuilder
| Feature | String | StringBuilder |
|---|---|---|
| Mutability | Immutable (cannot be changed after creation) | Mutable (can be changed without creating new objects) |
| Performance | Slower for frequent modifications | Faster for frequent modifications |
| Memory Usage | Creates a new object for each modification | Modifies the same object, reducing memory overhead |
| Use Case | Use for small or infrequently modified strings | Use for large or frequently modified strings |
| Modification Methods | Modification requires creating a new string | Modification is done in-place |
| Thread Safety | Strings are thread-safe | StringBuilder is not inherently thread-safe |
Example: Demonstrating the differences between String and StringBuilder
using System;
using System.Text;
using System.Collections;
class Geeks {
// Concatenates to String
public static void concat1(String s1)
{
// taking a string which
// is to be Concatenate
String st = "forGeeks";
// using String.Concat method
// you can also replace it with
// s1 = s1 + "forgeeks";
s1 = String.Concat(s1, st);
}
// Concatenates to StringBuilder
public static void concat2(StringBuilder s2)
{
// using Append method
// of StringBuilder class
s2.Append("forGeeks");
}
// Main Method
public static void Main(String[] args)
{
String s1 = "Geeks";
concat1(s1); // s1 is not changed
Console.WriteLine("Using String Class: " + s1);
StringBuilder s2 = new StringBuilder("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.
- Use of concat2 Method: In this method, we are passing a string “Geeks” and performing “s2.Append(“forGeeks”)” which modifies the same
StringBuilderobject by appending new content, as it is mutable.
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:
using System;
using System.Text;
class Geeks
{
// Main Method
public static void Main(String[] args)
{
String str = "Geeks";
// conversion from String object
// to StringBuilder
StringBuilder sbl = new StringBuilder(str);
sbl.Append("ForGeeks");
Console.WriteLine(sbl);
}
}
Output
GeeksForGeeks
Converting StringBuilder to String
This conversions can be performed using ToString() method.
Example:
using System;
using System.Text;
class Geeks
{
// Main Method
public static void Main(String[] args)
{
StringBuilder sbdr = new StringBuilder("Builder");
// conversion from StringBuilder
// object to String using ToString method
String str1 = sbdr.ToString();
Console.Write("StringBuilder object to String: ");
Console.WriteLine(str1);
}
}
Output
StringBuilder object to String: Builder