Open In App

StringBuilder in C#

Improve
Improve
Like Article
Like
Save
Share
Report

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.

Declaration and Initialization of StringBuilder

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.

Defining the capacity 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)

StringBuilder.Append(string value) Method

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) method
using 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);
    }
}


Output:

HELLO GFGGEEKS
GeeksForGeeks

StringBuilder.AppendFormat()

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() method
using 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);
    }
}


Output:

Your total amount is ¤50.00

StringBuilder.Insert(int index, string value) method

This method inserts the string at specified index in StringBuilder object.

Example:




// C# program to demonstrate the 
// StringBuilder.Insert(int index,
// string value) method
using 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);
    }
}


Output:

HELLO GEEKS

StringBuilder.Remove(int start, int length) Method

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) method
using 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);
    }
}


Output:

GeeksGeeks

StringBuilder.Replace(old_val, new_val) Method

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) method
using 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);
    }
}


Output:

Geeks For Geeks


Last Updated : 11 Mar, 2019
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads