Updated March 18, 2023
Difference Between String vs StringBuilder
In this article, String vs StringBuilder represents the sequence of characters; the first difference between them is the String class is in the System namespace, whereas StringBuilder is in System. Text namespace.
The major difference is:
The string is Immutable: Once you create an instance of String, you cannot change it. If you change the string’s value, then this will keep the old instance and create a new instance with a new value in the memory.
StringBuilder is Mutable: If you change the value of string using StringBuilder, it will not create a new instance; instead, it will update the value in the existing instance.
Head to Head Comparison between String vs StringBuilder (Infographics)
Below are the top 8 comparisons between String and StringBuilder:
Key Differences Between String vs StringBuilder
Let us discuss some key differences between String vs StringBuilder in the following points:
1. While working with the String class, every time you perform some operations on your string, you recreate the entire string in the memory over and over again, whereas StringBuilder allocates some buffer space in the memory and applies modifications into that buffer space.
2. As the StringBuilder object is mutable, it provides better performance as compared to the String object when heavy string manipulations are involved.
Let us understand the above points with the help of an example given below:
Code:
using System;
using System.Text;
using System.Diagnostics;
public class StringAndStringBuilder
{
static void Main()
{
//using Stopwatch to calculate time
Stopwatch stopWatch = new Stopwatch();
string str = string.Empty;
//starting stop watch
stopWatch.Start();
for (int i = 0; i < 1000; i++)
{
str += i.ToString();
}
stopWatch.Stop();
Console.WriteLine("Time taken by string : {0}", stopWatch.Elapsed);
StringBuilder stringBuilder= new StringBuilder(string.Empty);
//restarting stop watch for StringBuilder
stopWatch.Restart();
for (int i = 0; i < 1000; i++)
{
stringBuilder.Append(i.ToString());
}
stopWatch.Stop();
Console.WriteLine("Time taken by StringBuilder : {0}", stopWatch.Elapsed);
}
}
Output:
3. String operations use more memory as compared to StringBuilder because String creates intermediate garbage instances after each operation. Let us see this in the below example:
Code:
using System;
using System.Text;
using System.Runtime.Serialization;
public class StringAndStringBuilder
{
public static void Main()
{
//using ObjectIDGenerator to generate ID for objects
ObjectIDGenerator idGenerator1 = new ObjectIDGenerator();
bool flag = new bool();
String str = "Sun";
Console.WriteLine("String = {0}", str);
Console.WriteLine("Instance Id : {0}", idGenerator1.GetId(str, out flag));
//flag will be True for new instance otherwise it will be False
Console.WriteLine("This instance is new : {0}\n", flag);
//concatenating strings
str += " rises";
Console.WriteLine("String = {0}", str);
Console.WriteLine("Instance Id : {0}", idGenerator1.GetId(str, out flag));
Console.WriteLine("this instance is new : {0}\n", flag);
str += " in";
Console.WriteLine("String = {0}", str);
Console.WriteLine("Instance Id : {0}", idGenerator1.GetId(str, out flag));
Console.WriteLine("this instance is new : {0}\n", flag);
str += " the";
Console.WriteLine("String = {0}", str);
Console.WriteLine("Instance Id : {0}", idGenerator1.GetId(str, out flag));
Console.WriteLine("this instance is new : {0}\n", flag);
str += " east";
Console.WriteLine("String = {0}", str);
Console.WriteLine("Instance Id : {0}", idGenerator1.GetId(str, out flag));
Console.WriteLine("this instance is new : {0}\n", flag);
//initializing string using StringBuilder
StringBuilder stringBuilder = new StringBuilder("Sun");
ObjectIDGenerator idGenerator2 = new ObjectIDGenerator();
Console.WriteLine("StringBuilder = {0}", stringBuilder);
Console.WriteLine("Instance Id : {0}", idGenerator2.GetId(stringBuilder, out flag));
Console.WriteLine("This instance is new : {0}\n", flag);
stringBuilder.Append(" rises");
Console.WriteLine("StringBuilder = {0}", stringBuilder);
Console.WriteLine("Instance Id : {0}", idGenerator2.GetId(stringBuilder, out flag));
Console.WriteLine("This instance is new : {0}\n", flag);
stringBuilder.Append(" in");
Console.WriteLine("StringBuilder = {0}", stringBuilder);
Console.WriteLine("Instance Id : {0}", idGenerator2.GetId(stringBuilder, out flag));
Console.WriteLine("This instance is new : {0}\n", flag);
stringBuilder.Append(" the");
Console.WriteLine("StringBuilder = {0}", stringBuilder);
Console.WriteLine("Instance Id : {0}", idGenerator2.GetId(stringBuilder, out flag));
Console.WriteLine("This instance is new : {0}\n", flag);
stringBuilder.Append(" east");
Console.WriteLine("StringBuilder = {0}", stringBuilder);
Console.WriteLine("Instance Id : {0}", idGenerator2.GetId(stringBuilder, out flag));
Console.WriteLine("This instance is new : {0}\n", flag);
Console.ReadKey();
}
}
Output:
Pictorial representation of the internal working of the above program:
4. String objects can be used across threads, whereas StringBuilder objects do not thread-safe. Thus, two or more threads can call StringBuilder methods simultaneously.
5. As String objects are thread-safe, they provide synchronization, whereas StringBuilder does not provide synchronization.
6. String class is used when the value of the string is going to remain constant throughout the program or when there are very fewer modifications required on it, whereas StringBuilder is used when many repeated modifications or heavy operations are required to perform on the string.
7. For a very less number of concatenations, the String class works faster as compared to StringBuilder because accommodating characters in buffer space and then converting the entire value to string again creates overhead. Thus, for the small number of concatenations, StringBuilder works slower as compared to the copy-by-value behaviour of the String class.
8. To convert the String object to the StringBuilder object, we need to pass the String object to the StringBuilder class constructor, whereas the StringBuilder object can be converted to String using the ToString() method.
String str = "abc";
StringBuilder StringBuilder = new StringBuilder(str);
StringBuilder StringBuilder = new StringBuilder("abc");
String str = StringBuilder.ToString();
String vs StringBuilder Comparison Table
The table below summarizes the comparisons between String vs StringBuilder:
Parameter | String | StringBuilder |
Namespace | The string class is in the System namespace. | StringBuilder class is in the System Text namespace. |
Mutability | The string created using the String class is immutable. | The string created using StringBuilder is mutable. |
Performance | Slow as compared to StringBuilder. | Fast as compared to String. Like it is faster than String while concatenating many strings together in a loop. |
Thread Safety | Thread-safe. | Not thread-safe. |
Synchronization | Provides synchronization. | It does not provide synchronization. |
Memory Usage | Requires more memory to create a new instance if operations performed on a string change its value. | It requires less memory as it updates the existing instance. |
Nature of Object | An object of String is read-only in nature. | An object of StringBuilder is dynamic in nature. |
Efficiency | String class is less efficient as compared to StringBuilder while working with a large number of string concatenations. | StringBuilder is more efficient as compared to String while working with a large number of string concatenations. |
Conclusion
String creates a new instance only when the value of the string changes. If you perform certain operations on a string that does not change its value, then String will not create any new instance creation. Two or more string literals with the same content may have the same memory location.
Recommended Articles
This is a guide to the top difference between String vs StringBuilder. Here we also discuss the String vs StringBuilder key differences with infographics and comparison table. You may also have a look at the following articles to learn more –