How to use StringBuilder in C#?

The StringBuilder class in C# is designed for efficient string manipulation when you need to perform multiple operations like appending, inserting, or replacing characters. Unlike regular strings, which are immutable and create new objects for every modification, StringBuilder maintains a mutable buffer that can be expanded without creating new objects in memory.

Syntax

Following is the syntax to initialize a StringBuilder

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

Key Advantages of StringBuilder

  • Mutable − Can modify content without creating new objects

  • Memory efficient − Reduces memory allocation and garbage collection

  • Performance − Faster for multiple string operations

  • Capacity management − Automatically expands buffer as needed

String vs StringBuilder String (Immutable) "Hello" "Hello World" "Hello World!" 3 separate objects created in memory StringBuilder (Mutable) Buffer: [Hello ] Buffer: [Hello World] Buffer: [Hello World!] Same buffer modified in-place

Using StringBuilder for String Replacement

Example

using System;
using System.Text;

public class Program {
   public static void Main() {
      StringBuilder str = new StringBuilder("Web World!!", 30);
      Console.WriteLine("Original: " + str);
      
      str.Replace("World", "Arena");
      Console.WriteLine("After Replace: " + str);
   }
}

The output of the above code is −

Original: Web World!!
After Replace: Web Arena!!

Using StringBuilder for Multiple Operations

Example

using System;
using System.Text;

public class Program {
   public static void Main() {
      StringBuilder sb = new StringBuilder("Hello", 50);
      Console.WriteLine("Initial: " + sb);
      Console.WriteLine("Capacity: " + sb.Capacity);
      Console.WriteLine("Length: " + sb.Length);
      
      sb.Append(" World");
      sb.Append("!");
      sb.Insert(5, " Beautiful");
      Console.WriteLine("Final: " + sb);
      Console.WriteLine("Length: " + sb.Length);
   }
}

The output of the above code is −

Initial: Hello
Capacity: 50
Length: 5
Final: Hello Beautiful World!
Length: 22

Common StringBuilder Methods

Method Description
Append() Adds text to the end of the StringBuilder
Insert() Inserts text at a specified position
Replace() Replaces all occurrences of a specified string
Remove() Removes characters from a specified position
ToString() Converts StringBuilder to a string

Performance Comparison Example

Example

using System;
using System.Text;
using System.Diagnostics;

public class Program {
   public static void Main() {
      int iterations = 1000;
      
      // Using String concatenation
      Stopwatch sw1 = Stopwatch.StartNew();
      string str = "";
      for (int i = 0; i < iterations; i++) {
         str += "A";
      }
      sw1.Stop();
      
      // Using StringBuilder
      Stopwatch sw2 = Stopwatch.StartNew();
      StringBuilder sb = new StringBuilder();
      for (int i = 0; i < iterations; i++) {
         sb.Append("A");
      }
      string result = sb.ToString();
      sw2.Stop();
      
      Console.WriteLine("String concatenation: " + sw1.ElapsedMilliseconds + " ms");
      Console.WriteLine("StringBuilder: " + sw2.ElapsedMilliseconds + " ms");
      Console.WriteLine("StringBuilder is faster!");
   }
}

The output of the above code is −

String concatenation: 15 ms
StringBuilder: 0 ms
StringBuilder is faster!

Conclusion

StringBuilder in C# is essential for efficient string manipulation when performing multiple operations. It provides better performance and memory usage compared to regular string concatenation by maintaining a mutable buffer that can be modified in-place without creating new objects.

Updated on: 2026-03-17T07:04:35+05:30

368 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements