What is StringBuilder in C# and How to Use it

StringBuilder in C# with Examples

This tutorial describes what StringBuilder is in C# and how to use it.

A StringBuilder is a class that presents a mutable string of characters.


More C# tutorials:


StringBuilder Initialization

You can initialize a new instance of the StringBuilder class in the following ways:

Initialize a new instance of the StringBuilder:

StringBuilder stringBuilder = new StringBuilder();

Initialize a new instance of the StringBuilder with capacity:

StringBuilder stringBuilder = new StringBuilder(int capacity);

Initialize a new instance of the StringBuilder with a default value:

StringBuilder stringBuilder = new StringBuilder(string value);

Initialize a new instance of the StringBuilder with a default value and capacity:

StringBuilder stringBuilder = new StringBuilder(string value, int capacity);


StringBuilder Methods

ToString Method

ToString is a method to convert the value of a StringBuilder object to a String.

The syntax of the ToString method is as follows:

stringBuilder.ToString()


Append Method

To append a StringBuilder, use the following syntax:

stringBuilder.Append(value)

Example:

The following example instantiates a new StringBuilder object and appends it:

StringBuilder stringBuilder = new StringBuilder("Hello ", 20);

stringBuilder.Append("World!");

stringBuilder.Append(true);

stringBuilder.Append(100);

using System;
using System.Text;
 
namespace StringBuilderExample
{
    class Program
    {
        static void Main(string[] args)
        {
            StringBuilder stringBuilder = new StringBuilder("Hello", 20);
            Console.WriteLine("Before: " + stringBuilder.ToString());
            stringBuilder.Append("World!");
            Console.WriteLine("After appending World!: " + stringBuilder.ToString());
            stringBuilder.Append(true); 
            Console.WriteLine("After appending true: " + stringBuilder.ToString());
            stringBuilder.Append(100);
            Console.WriteLine("After appending 100: " + stringBuilder.ToString());
        }
    }
}

Output:

Before: Hello
After appending World!: HelloWorld!
After appending true: HelloWorld!True
After appending 100: HelloWorld!True100

Insert Method

To insert a value into a StringBuilder at a specified character position, use the following syntax:

stringBuilder.Insert(index, value)

Example:

The following illustrates an example of inserting at position 5, 7, and 10:

StringBuilder stringBuilder = new StringBuilder("This is a tutorial about C# StringBuilder class.", 100);

stringBuilder.Insert(5, "Hello");

stringBuilder.Insert(7, 'A');

stringBuilder.Insert(10, 200.50);

using System;
using System.Text;
 
namespace StringBuilderExample
{
    class Program
    {
        static void Main(string[] args)
        {
            StringBuilder stringBuilder = new StringBuilder("This is a tutorial about C# StringBuilder class.", 100);
            Console.WriteLine("Before: " + stringBuilder.ToString());
            stringBuilder.Insert(5, "Hello");
            Console.WriteLine("After inserting Hello: " + stringBuilder.ToString());
            stringBuilder.Insert(7, 'A');
            Console.WriteLine("After inserting A: " + stringBuilder.ToString());
            stringBuilder.Insert(10, 200.50);
            Console.WriteLine("After inserting 200.50: " + stringBuilder.ToString());
        }
    }
}

Output:

Before: This is a tutorial about C# StringBuilder class.
After inserting Hello: This Hellois a tutorial about C# StringBuilder class.
After inserting A: This HeAllois a tutorial about C# StringBuilder class.
After inserting 200.50: This HeAll200.5ois a tutorial about C# StringBuilder class.

Another example:

using System;
using System.Text;
 
namespace StringBuilderExample
{
    class Program
    {
        static void Main(string[] args)
        {
            StringBuilder stringBuilder = new StringBuilder("Learn C# programming", 50);
            Console.WriteLine("Before: " + stringBuilder.ToString());
            stringBuilder.Insert(0, "I ");
            stringBuilder.Insert(22, '.');
            Console.WriteLine("After: " + stringBuilder.ToString());
        }
    }
}

Output:

Before: Learn C# programming
After: I Learn C# programming.

Remove Method

To remove the specified range of characters from a StringBuilder, use the following syntax:

stringBuilder.Remove(int startIndex, int length)

Example:

The following code removes six characters starting at position 5:

StringBuilder stringBuilder = new StringBuilder("This is a tutorial about C# StringBuilder class.", 100);

stringBuilder.Remove(5, 6);

using System;
using System.Text;
 
namespace StringBuilderExample
{
    class Program
    {
        static void Main(string[] args)
        {
            StringBuilder stringBuilder = new StringBuilder("This is a tutorial about C# StringBuilder class.", 100);
            Console.WriteLine("Before: " + stringBuilder.ToString());
            stringBuilder.Remove(5, 6);
            Console.WriteLine("After: " + stringBuilder.ToString());
        }
    }
}

Output:

Before: This is a tutorial about C# StringBuilder class.
After: This utorial about C# StringBuilder class.

Clear Method

To remove all characters from a StringBuilder object, use the following syntax:

stringBuilder.Clear();

Example:

The following example instantiates a new StringBuilder object, with a default value "Hello World" and capacity 100, and clears all characters using the Clear method:

StringBuilder stringBuilder = new StringBuilder("Hello World!", 100);

stringBuilder.Clear();

using System;
using System.Text;
 
namespace StringBuilderExample
{
    class Program
    {
        static void Main(string[] args)
        {
            StringBuilder stringBuilder = new StringBuilder("Hello World!", 100); 
            Console.WriteLine("Before: " + stringBuilder.ToString());
            stringBuilder.Clear();
            Console.WriteLine("After: " + stringBuilder.ToString());
        }
    }
}

Output:

Before: Hello World!
After:

Replace Method

You can replace all occurrences of a specified string with another string by using one of the following syntaxes:

To replace all occurrences of a specified string with another specified string, use the following syntax:

stringBuilder.Replace(string oldValue, string newValue);

Example:

using System;
using System.Text;
 
namespace StringBuilderExample
{
    class Program
    {
        static void Main(string[] args)
        {
            StringBuilder stringBuilder = new StringBuilder("This tutorial, this tutorial, this tutorial", 100);
            Console.WriteLine("Before replacing: " + stringBuilder.ToString());
            //Replace
            stringBuilder.Replace("tutorial", "guide");
            Console.WriteLine("After replacing: " + stringBuilder.ToString());
        }
    }
}

Output:

Before replacing: This tutorial, this tutorial, this tutorial
After replacing: This guide, this guide, this guide

If you need to replace all occurrences of a specified string, within a substring, with another specified string, you can use the following syntax:

stringBuilder.Replace(string oldValue, string newValue, int startIndex, int count);

Example:

The following example replaces all occurrences of the tutorial word, within a substring starting at position 15 to 34, with a guide:

using System;
using System.Text;
 
namespace StringBuilderExample
{
    class Program
    {
        static void Main(string[] args)
        {
            StringBuilder stringBuilder = new StringBuilder("This tutorial, this tutorial, this tutorial", 100);
            Console.WriteLine("Before replacing: " + stringBuilder.ToString());
            //Replace
            stringBuilder.Replace("tutorial", "guide", 15, 20);
            Console.WriteLine("After replacing: " + stringBuilder.ToString());
        }
    }
}

Output:

Before replacing: This tutorial, this tutorial, this tutorial
After replacing: This tutorial, this guide, this tutorial

Use the following syntax to replace all occurrences of a specified character with another character:

stringBuilder.Replace(char oldValue, char newValue);

Or use the following syntax to replace all occurrences of a specified character with another character within a substring:

stringBuilder.Replace(char oldValue, char newValue, int startIndex, int count);

In this tutorial, you have learned how to use the StringBuilder class in C#.
StringBuilder is a class that represents a mutable string of characters. You can append, remove, insert, or clear by using the StringBuilder built-in methods.


See also:
C# Read Text File with StreamReader
C# Write to File with the File Class and StreamWriter
C# switch case Statements with Examples
C# if-else Statements with Examples
C# while Loop Statement with Examples