StringBuilder is a class in C# that helps you work with strings in a more efficient way. Unlike regular strings, StringBuilder allows you to modify text without creating new objects in memory every time. It is found in the System.Text namespace.
Example: Creating Basic StringBuilder
using System;
using System.Text;
class Program
{
static void Main()
{
// Create with initial text
StringBuilder sb = new StringBuilder("Hello");
Console.WriteLine(sb.ToString());
}
}
Output
Hello
When to Use StringBuilder
- You need to perform many string operations (like concatenation)
- You are building strings in loops
- Working with large amounts of text data
- Performance is important in your application
StringBuilder Constructors
StringBuilder provides several constructors to create objects in different ways:
1. Default Constructor
Creates an empty StringBuilder with default capacity (16 characters).
StringBuilder sb = new StringBuilder();
2. Constructor with String
Creates StringBuilder with initial string content.
StringBuilder sb = new StringBuilder("Hello World");
3. Constructor with Capacity
Creates StringBuilder with specified initial capacity.
StringBuilder sb = new StringBuilder(100); // Initial capacity of 100 characters
4. Constructor with String and Capacity
Creates StringBuilder with initial string and specified capacity.
StringBuilder sb = new StringBuilder("Hello", 50);
5. Constructor with String, Start Index, Length and Capacity
Creates StringBuilder using part of a string with specified capacity.
string text = "Hello World";
StringBuilder sb = new StringBuilder(text, 0, 5, 20); // Takes "Hello", capacity 20
StringBuilder Methods
| Method | Description |
|---|---|
| Append(value) | Adds text, number or object to the end of StringBuilder. |
| AppendLine(value) | Adds text and a new line character at the end. |
| Insert(index, value) | Inserts text at the specified position. |
| Remove(start, length) | Removes a specified number of characters starting from the given position. |
| Replace(old, new) | Replaces all occurrences of the specified character or string. |
| Clear() | Removes all characters from StringBuilder. |
| ToString() | Converts StringBuilder content to a regular string. |
| AppendFormat(format, args) | Adds a formatted string (like string.Format). |
| CopyTo(index, array, start, count) | Copies characters to a character array. |
| EnsureCapacity(capacity) | Ensures StringBuilder can hold the specified number of characters. |
Common Method Examples
1. Append() Method
Adds text to the end of StringBuilder.
StringBuilder sb = new StringBuilder("Hello");
sb.Append(" World");
sb.Append("!");
Console.WriteLine(sb.ToString()); // Output: Hello World!
2. Insert() Method
Adds text at a specific position.
StringBuilder sb = new StringBuilder("Hello World");
sb.Insert(6, "Beautiful ");
Console.WriteLine(sb.ToString()); // Output: Hello Beautiful World
3. Remove() Method
Removes characters from StringBuilder.
StringBuilder sb = new StringBuilder("Hello World");
sb.Remove(5, 6); // Remove 6 characters starting from index 5
Console.WriteLine(sb.ToString()); // Output: Hello
4. Replace() Method
StringBuilder sb = new StringBuilder("Hello World");
sb.Replace("World", "C#");
Console.WriteLine(sb.ToString()); // Output: Hello C#
Important Properties
1. Length Property
Gets or sets the number of characters.
StringBuilder sb = new StringBuilder("Hello");
Console.WriteLine(sb.Length); // Output: 5
sb.Length = 3;
Console.WriteLine(sb.ToString()); // Output: Hel
2. Capacity Property
Gets or sets the maximum number of characters that can be stored.
StringBuilder sb = new StringBuilder();
Console.WriteLine(sb.Capacity); // Default capacity
sb.Capacity = 50;
Console.WriteLine(sb.Capacity); // Output: 50
3. MaxCapacity Property
Gets the maximum capacity of StringBuilder.
StringBuilder sb = new StringBuilder();
Console.WriteLine(sb.MaxCapacity); // Shows maximum possible capacity