StringBuilder Class

System.Text

Represents a mutable sequence of characters. This class cannot be inherited. It is designed for scenarios where you need to perform many append or insert operations on a string. StringBuilder provides performance advantages over String concatenation because it avoids the creation of multiple intermediate String objects.

Summary

The StringBuilder class is part of the System.Text namespace and is used to build strings efficiently. Unlike immutable String objects, StringBuilder objects can be modified by appending, inserting, deleting, or replacing characters. This makes it significantly more performant for operations that involve frequent string manipulation.

Syntax
public sealed class StringBuilder
Properties

Capacity

public int Capacity { get; set; }
Gets or sets the current capacity of the StringBuilder object, which is the sum of the current number of characters plus the remaining space available for allocation before the object's length is increased.

HasChanges

public bool HasChanges { get; }
Indicates whether the current StringBuilder instance has been modified since its creation.

Length

public int Length { get; set; }
Gets or sets the current number of characters in the StringBuilder object.
Constructors

StringBuilder()

public StringBuilder()
Initializes a new instance of the StringBuilder class. The default capacity is 16 characters.

StringBuilder(int capacity)

public StringBuilder(int capacity)
Initializes a new instance of the StringBuilder class with a specified initial capacity.

StringBuilder(int capacity, int maxCapacity)

public StringBuilder(int capacity, int maxCapacity)
Initializes a new instance of the StringBuilder class with a specified initial capacity and maximum capacity.
Methods

Append(string value)

public StringBuilder Append(string value)
Appends the string representation of a value to this instance.
Parameters:
  • value: The string to append.
Returns: A reference to this instance after the append operation.

Append(char value)

public StringBuilder Append(char value)
Appends the specified character to the end of this instance.
Returns: A reference to this instance after the append operation.

Insert(int index, string value)

public StringBuilder Insert(int index, string value)
Inserts a string into this instance at the specified index.
Parameters:
  • index: The zero-based index at which to insert.
  • value: The string to insert.
Returns: A reference to this instance after the insert operation.

Remove(int startIndex, int length)

public StringBuilder Remove(int startIndex, int length)
Removes a specified number of characters from the current position in this instance and replaces them with characters from the specified array.
Parameters:
  • startIndex: The position within this instance where the removal begins.
  • length: The number of characters to remove.
Returns: A reference to this instance after the remove operation.

ToString()

public override string ToString()
Converts this instance to its string representation.
Returns: A string that represents the value of this instance.
Examples

The following C# code example demonstrates how to use the StringBuilder class:

using System;
using System.Text;

public class Example
{
    public static void Main(string[] args)
    {
        // Initialize a StringBuilder
        StringBuilder sb = new StringBuilder("Hello");

        // Append more text
        sb.Append(" ");
        sb.Append("world!");

        // Insert text at a specific position
        sb.Insert(5, " beautiful");

        // Remove some characters
        sb.Remove(0, 6); // Removes "Hello "

        // Convert to string and print
        Console.WriteLine(sb.ToString());
        // Output: beautiful world!
    }
}
Remarks

When you create a StringBuilder object, you can specify its initial capacity. If the number of characters appended exceeds the capacity, the internal buffer is reallocated to accommodate the new characters. The Capacity property can be used to retrieve or set the current capacity.

The Length property indicates the number of characters currently in the StringBuilder. Setting the Length to a value less than the current length truncates the string. Setting it to a value greater than the current length pads the string with null characters.