In this Article, We Discuss What is String and String Builder, When and Where To Use String and StringBuilder and which is Better To use and Difference Between string and StringBuilder?
String is Immutable. Immutable means once a string is declared cannot be changed. Then the next question that comes to our mind is “If String is immutable then how am I able to change the string value of the object whenever I want to?”. Well, to be precise it’s not the same String object that reflects the changes you do. Internally one new string object will be created to perform the changes
string str="hello"; //Here you Declare a String
//now you want to concatenate "world" to the above declared string. What do you do?
str=str+"world"; //Here you Concatenate the new Word To the Previous String
when you print string str then the output will be "Hello World".
Although we made the use of the same string(str). But internally new string object is created in the process. So if you do modification in the same string object using append or by using any other method call, you would really be creating those many new objects of class String. In other words, many copies of the string are created in the memory and one original string is also stored in memory.
When you have a static string value or when you have a few modifications in the string value. Then, String is recommended
StringBuilder is mutable. Mutable means are modifiable. When you declared a StringBuilder, 16-character memory is allocated by default, and when these 16 characters are used in StringBuilder, it is automatically resized to double in length. So a new Object is not created every time for the new string value.
StringBuilder is not synchronized and not thread-safe
//First you Declare a string
StringBuilder sb=new StringBuilder();
//Now Add value to StringBuilder using Append()
Sb.append(“hello”);
//Here Hello Contains 5 Character,now 11 character is Remained
//Now again ,append new value to StringBuilder
Sb.append(“ world”);
//Here more 6 character is used from remaining 11 character, now 5 character is remained, now add “America” to StringBuilder
sb.append(“ america”);
//Here total character of string “Hello World America” is more than 16,so StringBuilder Double its length, now StringBuilder length is 32 characters
When you want to frequent changes in the String value. Then, StringBuilder is suggested.
StringBuilder sb=new StringBuilder(10000);
Here we declared 10000 characters capacity or length of StringBuilder And After Declared Capacity is used, StringBuilder Automatically Double its Length or Capacity to 20000 or so on.
using System;
using System.Text;
using System.Diagnostics;
namespace stringdiff
{
class Program
{
static void Main(string[] args)
{
Stopwatch str_watch = new Stopwatch(); //for Tracing time duration for concatenating string
str_watch.Start();
string a = "";
for (int i = 0; i < 100000; i++)
{
a = a + i;
}
str_watch.Stop();
Console.WriteLine("Time elapsed For String:- " + str_watch.Elapsed.Milliseconds +"ms");
Stopwatch sb_watch = new Stopwatch(); //for Tracing time duration for concatenating string
sb_watch.Start();
StringBuilder sb = new StringBuilder();
for (int i = 0; i < 100000; i++)
{
sb.Append(i);
}
sb_watch.Stop();
Console.WriteLine("Time elapsed For StringBuilder:- " + sb_watch.Elapsed.Milliseconds +"ms");
Console.ReadKey();
}
}
}
String | StringBuilder |
String is Immutable, once created cannot be changed. | StringBuilder is mutable, Mutable means Modifiable. |
Performance is slower than StringBuilder. | Performance is faster than string. |
String is thread-safe. | StringBuilder is not synchronized and not thread-safe |
Using string, if you try to alter their values, another object gets created. | Using String, if you try to alter their values, No new object gets created. |
When you want to frequent changes in String value. Then, StringBuilder is suggested. | When you want to frequent changes in String value. Then, StringBuilder is suggested. |
When you want to frequent changes in the String value. Then, StringBuilder is suggested. And When you have a static string value or when you have a few modifications in the string value. Then, String is recommended.
Comments