StringBuilder in C# - GeeksforGeeks

11 Mar, 2019
C# StringBuilder is similar to Java StringBuilder. A String object is immutable, i.e. a String cannot be changed once created. Every time when you use any of the methods of the System.String class, then you create a new string object in memory. For example, a string “GeeksForGeeks” occupies memory in the heap, now, by changing the initial string “GeeksForGeeks” to “GFG” will create a new string object on the memory heap instead of modifying the initial string at the same memory location. In situations where you need to perform repeated modifications to a string, we need StringBuilder class. To avoid string replacing, appending, removing or inserting new strings in the initial string C# introduce StringBuilder concept. StringBuilder is a dynamic object. It doesn’t create a new object in the memory but dynamically expands the needed memory to accommodate the modified or new string.
StringBuilder can be declared and initialized the same way as class,
StringBuilder s = new StringBuilder();
            
or

StringBuilder s = new StringBuilder("GeeksforGeeks");

“s” is the object of StringBuilder class. Also, we can pass a string value(here “GeeksforGeeks”) as an argument to the constructor of StringBuilder.
Although the StringBuilder is a dynamic object that allows you to expand the number of characters in the string that it encapsulates, you can specify a value for the maximum number of characters that it can hold. This value is called the capacity of the StringBuilder object.
StringBuilder s = new StringBuilder(20);

or

StringBuilder s = new StringBuilder("GeeksForGeeks", 20);

Here,
In the 1st statement we pass an integer value as an argument to the constructor. This is the maximum capacity of character that can hold a string.
In the 2nd statement we pass string value with an integer value (that is the maximum capacity of character a string can hold) as an argument to the constructor.
.
Important Methods of StringBuilder Class:
Append(string value)
AppendFormat()
Insert(int index, string value)
Remove(int start, int length)
Replace(old_val, new|_val)
The Append method can be used to add or append a string value of an object to the end of a string represented by the current StringBuilder object. AppendLine() method also come under this method. This method append the string with a newline at the end.
Example:
// C# program to demonstrate the // StringBuilder.Append(value) and// StringBuilder.AppendLine(value) methodusing System;using System.Text;  class GFG {      // Main Method    public static void Main()    {          // "20" is capacity        StringBuilder s = new StringBuilder("HELLO ", 20);                  s.Append("GFG");          // after printing "GEEKS"        // a new line append        s.AppendLine("GEEKS");                  s.Append("GeeksForGeeks");        Console.WriteLine(s);    }}
HELLO GFGGEEKS
GeeksForGeeks

This method uses to format the input string into the specified format and then append it. This method also appends text to the end of the StringBuilder object.
// C# program to demonstrate the // StringBuilder.AppendFormat() methodusing System;using System.Text;  class GFG {      // Main Method    public static void Main()    {        StringBuilder s = new StringBuilder("Your total amount is ");          // using the method        s.AppendFormat("{0:C} ", 50);          Console.WriteLine(s);    }}
Your total amount is Â¤50.00

This method inserts the string at specified index in StringBuilder object.
Example:
// C# program to demonstrate the // StringBuilder.Insert(int index,// string value) methodusing System;using System.Text;  class GFG {      // Main Method    public static void Main()    {          // "20" is capacity        StringBuilder s = new StringBuilder("HELLO ", 20);                  // "GEEKS" insert after 6th index        s.Insert(6, "GEEKS");                  Console.WriteLine(s);    }}
HELLO GEEKS

This method removes the specified number of characters from the current StringBuilder object. The removing process beginning at a specified index and extends up to another specified index.
Example:
// C# program to demonstrate the // StringBuilder.Remove(int index,// int length) methodusing System;using System.Text;  class GFG {      // Main Method    public static void Main()    {          // "20" is capacity        StringBuilder s = new StringBuilder("GeeksForGeeks", 20);          // remove starts from index 5        // and remove happes 3 index         // after index 5        s.Remove(5, 3);                  Console.WriteLine(s);    }}
GeeksGeeks

This method is used to replace characters within the StringBuilder object with another specified character.
Example:
// C# program to demonstrate the // StringBuilder.Replace(string old_val,// string new_val) methodusing System;using System.Text;  class GFG {      // Main Method    public static void Main()    {          // "20" is capacity        StringBuilder s = new StringBuilder("GFG Geeks ", 20);                  // Replace "GFG" with "Geeks For"        s.Replace("GFG", "Geeks For");          Console.WriteLine(s);    }}
Geeks For Geeks

CSharp-string
CSharp-StringBuilder-Class
Picked
C#
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Difference between Abstract Class and Interface in C#
C# | How to check whether a List contains a specified element
C# | IsNullOrEmpty() Method
String.Split() Method in C# with Examples
Difference between Ref and Out keywords in C#
C# | Arrays of Strings
C# | Delegates
Top 50 C# Interview Questions & Answers
Extension Method in C#
C# | Abstract Classes