StringBuilder Class
StringBuilder
classSummary
Represents a mutable string of characters.
The StringBuilder class is a mutable sequence of characters. It is similar to the String class, but it is more efficient when you need to perform a series of modifications to a string. For example, if you are concatenating strings in a loop, using StringBuilder can be significantly faster than repeatedly concatenating String objects.
Remarks
StringBuilder objects are designed for situations where you need to build a string by appending or inserting characters, substrings, or other objects.
Unlike String objects, which are immutable, StringBuilder objects are mutable. This means that operations that modify a StringBuilder object do not create a new object; they modify the existing object in place. This can lead to performance improvements when dealing with large strings or performing many modifications.
The capacity of a StringBuilder object is the number of characters that the object can hold before its internal buffer must be reallocated. The Capacity property can be set to a specific value. If the length of the string that is being built exceeds the capacity, the capacity is automatically increased.
Syntax
public sealed class StringBuilder
public StringBuilder (string value);
public StringBuilder (int capacity);
Members
| Name | Description |
|---|---|
| StringBuilder() | Initializes a new instance of the StringBuilder class to the default initial size. |
| StringBuilder(string value) | Initializes a new instance of the StringBuilder class by using the specified string. |
| StringBuilder(int capacity) | Initializes a new instance of the StringBuilder class by using the specified initial capacity. |
| Capacity | Gets or sets the current number of characters the instance can hold without reallocation. |
| Length | Gets or sets the length, in characters, of the current StringBuilder. |
| Append(...) | Appends the string representation of a specified value to the end of this instance. Overloaded for various types. |
| Insert(...) | Inserts the string representation of a specified value at a specified position in this instance. Overloaded for various types. |
| Remove(int startIndex, int length) | Removes a specified number of characters from the current StringBuilder instance, starting at a specified position. |
| ToString() | Converts this instance to its string representation. |
StringBuilder()
Initializes a new instance of the StringBuilder class to the default initial size.
StringBuilder(string value)
Initializes a new instance of the StringBuilder class by using the specified string.
Parameters
| Name | Type | Description |
|---|---|---|
value |
string | The initial string to append to the StringBuilder. |
StringBuilder(int capacity)
Initializes a new instance of the StringBuilder class by using the specified initial capacity.
Parameters
| Name | Type | Description |
|---|---|---|
capacity |
int | The maximum number of characters that the new instance can hold before its internal buffer must be reallocated. |
Capacity Property
Gets or sets the current number of characters that the instance can hold without reallocation.
Length Property
Gets or sets the length, in characters, of the current StringBuilder.
Append(...) Methods
Appends the string representation of a specified value to the end of this instance. There are numerous overloads for appending various data types.
Example:
// Appends a string StringBuilder sb = new StringBuilder("Hello"); sb.Append(" World!"); // sb is now "Hello World!" // Appends an integer sb.Append(123); // sb is now "Hello World!123"
Insert(...) Methods
Inserts the string representation of a specified value at a specified position in this instance. Overloaded for various data types.
Example:
StringBuilder sb = new StringBuilder("Hello World!"); sb.Insert(6, "Beautiful "); // sb is now "Hello Beautiful World!"
Remove(int startIndex, int length) Method
Removes a specified number of characters from the current StringBuilder instance, starting at a specified position.
Parameters
| Name | Type | Description |
|---|---|---|
startIndex |
int | The position to begin removing characters. |
length |
int | The number of characters to remove. |
Example:
StringBuilder sb = new StringBuilder("Hello Beautiful World!"); sb.Remove(6, 10); // sb is now "Hello World!"
ToString() Method
Converts this instance to its string representation.
Returns
string: A string that represents the contents of this instance.
Example
The following example demonstrates how to use the StringBuilder class to build a string efficiently.
using System; using System.Text; public class Example { public static void Main() { StringBuilder sb = new StringBuilder(); sb.Append("This is the first part. "); sb.Append("This is the second part. "); sb.Insert(0, "Beginning: "); sb.Append("And this is the end."); Console.WriteLine(sb.ToString()); // Output: Beginning: This is the first part. This is the second part. And this is the end. } }