StringBuilder Class In C#

Strings are one of most frequent use data types in C#. Strings in C# are immutable means that once a string is created, it cannot be changed. No characters can be added or removed from it, nor can its length be changed.

The String object is immutable while StringBuilder object is mutable. Both String and StringBuilder are reference type. But String acts like a value type.

StringBuilder is located in the System.Text namespace.

The System.Text.StringBuilder class can be used when you want to modify a string without creating a new object. Using the StringBuilder class can boost performance when concatenating many strings together in a loop.
 
The following table lists the methods you can use to modify the contents of a StringBuilder.
 
Method
Description
StringBuilder.Append
Appends information to the end of the current StringBuilder.
StringBuilder.AppendFormat
Replaces a format specifier passed in a string with formatted text.
StringBuilder.Insert
Inserts a string or object into the specified index of the current StringBuilder.
StringBuilder.Remove
Removes a specified number of characters from the current StringBuilder.
StringBuilder.Replace
Replaces a specified character at a specified index.
 

Program to explain StringBuilder Class

  1. using System;  
  2. using System.Text;  
  3. namespace StringBuilder_example  
  4. {  
  5.     class Program  
  6.     {  
  7.         static void Main(string[] args)  
  8.         {  
  9.             StringBuilder sbstr = new StringBuilder("We are the world");  
  10.             Console.WriteLine(sbstr);             
  11.   
  12.             sbstr.Append(" we are the people");  
  13.             Console.WriteLine(sbstr);  
  14.   
  15.             float currency=3400.50f;  
  16.             sbstr.AppendFormat(" Our individual salary : {0:C}. ", currency);  
  17.             Console.WriteLine(sbstr);  
  18.   
  19.             sbstr.Insert(11, "people of ");  
  20.             Console.WriteLine(sbstr);  
  21.   
  22.             sbstr.Remove(11, 10);  
  23.             Console.WriteLine(sbstr);  
  24.   
  25.             sbstr.Replace("world""Indian");  
  26.             Console.WriteLine(sbstr);  
  27.   
  28.             Console.ReadKey();  
  29.         }  
  30.     }  
  31. }  

Output of above program


StringBuilder Class in C#

Difference between String and StringBuilder class


String
StringBuilder
System.String is immutable
System.StringBuilder is mutable
Concatenation is used to combine two strings
Append method is used.
The first string is combined to the other string by creating a new copy in the memory as a string object, and then the old string is deleted
Insertion is done on the existing string.
String is efficient for small string manipulation
StringBuilder is more efficient in case large amounts of string manipulations have to be performed
 

Conclusion


I hope that this article would have helped you in understanding StringBuilder Class in C#. StringBuilder can improve the performance of your application.

Your feedback and constructive contributions are welcome. Please feel free to contact me for feedback or comments you may have about this article.


Similar Articles