C# - StringBuilder Object


C# introduced the StringBuilder in the System.Text namespace. In C#, a stringbuilder is also a sequence of characters that is used to represents text. Strings are reference types, which means they are allocated on the heap and can be null. They are also called mutable type. It means that you can modify its value without creating a new object each time. StringBuilder performs faster than string when appending multiple string values.

C# String Builder

Here is an example of a stringbuilder in C#.

StringBuilder sb = new StringBuilder("TutorialsTrend!!");

In this example, You can create an object of the StringBuilder class using the new keyword and passing an initial string.

Stringbuilder in C# have a number of built-in methods that allow you to manipulate and format them. Here are the few examples.

1. StringBuilder Length

A stringBuilder in C# is actually an object, which contain properties and methods that can perform certain operations on strings. For example, the length of a string can be found with the Length property.

 StringBuilder sb = new StringBuilder("TutorialsTrend!!");
 Console.WriteLine("Name Length is : " + sb.Length);    

2. Add/Append String to StringBuilder

Use the Append() method to append a string at the end of the current StringBuilder object. If a StringBuilder does not contain any string yet, it will add it. The AppendLine() method append a string with the newline character at the end.

StringBuilder sb = new StringBuilder("TutorialsTrend!!");
sb.Append("Hello ");
sb.AppendLine("Rohatash!");
sb.AppendLine("Welcome C#");
Console.WriteLine(sb);

3. Insert String into StringBuilder

Use the Insert() method inserts a string at the specified index in the StringBuilder object. Here is the example.

StringBuilder sb = new StringBuilder("TutorialsTrend!!");
Console.WriteLine("Name Length is : " + sb.Length);
sb.Append("Hello ");
sb.AppendLine("Rohatash!");
sb.AppendLine("Welcome C#");
Console.WriteLine(sb);
sb.Insert(5, " C#");
Console.WriteLine("Inserted string =" + sb);

4. Remove String in StringBuilder

Use the Remove() method to remove a string from the specified index and up to the specified length.

StringBuilder sb = new StringBuilder("TutorialsTrend!!");
Console.WriteLine("Name Length is : " + sb.Length);
sb.Append("Hello ");
sb.AppendLine("Rohatash!");
sb.AppendLine("Welcome C#");
Console.WriteLine(sb);
sb.Insert(5, " C#");
Console.WriteLine("Inserted string =" + sb);
sb.Remove(5,7);
Console.WriteLine("Removed string =" + sb);

5. Replace String in StringBuilder

Use the Replace() method to replace all the specified string occurrences with the specified replacement string.

StringBuilder sb = new StringBuilder("TutorialsTrend!");
Console.WriteLine("Name Length is : " + sb.Length);
sb.Append("Hello ");
sb.AppendLine("Rohatash!");
sb.AppendLine("Welcome C#");
Console.WriteLine(sb);
sb.Insert(5, " C#");
Console.WriteLine("Inserted string =" + sb);
sb.Remove(5,7);
Console.WriteLine("Removed string =" + sb);
sb.Replace("Trend", "Roh");
Console.WriteLine("Replace string ="+sb);

Here is the combined example of code all the above methods which used.

Example

using System;
using System.Text;

namespace FirstProgram
{
    class Program
    {
        static void Main(string[] args)
        {
            StringBuilder sb = new StringBuilder("TutorialsTrend!");
            Console.WriteLine("Name Length is : " + sb.Length);
            sb.Append("Hello ");
            sb.AppendLine("Rohatash!");
            sb.AppendLine("Welcome C#");
            Console.WriteLine(sb);
            sb.Insert(5, " C#");
            Console.WriteLine("Inserted string =" + sb);
            sb.Remove(5,7);
            Console.WriteLine("Removed string =" + sb);
            sb.Replace("Trend", "Roh");
            Console.WriteLine("Replace string ="+sb);
        }       
    }
}

Output

C# String Builder
Prev Next