StringBuilder Class
Represents a mutable sequence of characters.
Summary
The StringBuilder
class is used to create and modify strings. It is more efficient than the String
class when you need to perform many modifications to a string, such as appending or inserting characters. This is because String
objects are immutable, meaning that each modification creates a new string object. StringBuilder
objects, on the other hand, are mutable, and their internal buffer is modified directly, reducing the overhead of memory allocation and garbage collection.
Inheritance
Base Type |
---|
Object |
StringBuilder |
Constructors
StringBuilder()
Initializes a new instance of the StringBuilder
class using the default capacity (16) and default comparer (`StringComparer.CurrentCulture`).
StringBuilder(int capacity)
Initializes a new instance of the StringBuilder
class using the specified capacity and the default comparer (`StringComparer.CurrentCulture`).
Parameters
capacity
: The initial size of the buffer, in characters.
StringBuilder(string value)
Initializes a new instance of the StringBuilder
class to the specified value.
Parameters
value
: The string to initialize theStringBuilder
with.
Methods
Append(string value)
Appends the string representation of the specified value to this instance.
Parameters
value
: The string to append.
Returns
- A reference to this instance after the append operation has completed.
AppendLine(string value)
Appends a copy of the specified string followed by the default line terminator to the end of this instance.
Parameters
value
: The string to append.
Returns
- A reference to this instance after the append operation has completed.
ToString()
Converts the current StringBuilder
object to a string.
Returns
- A string that represents the contents of this instance.
Clear()
Removes all characters from the current StringBuilder
instance.
Properties
Capacity
Gets or sets the current capacity of the object.
Length
Gets or sets the current length of the string builder.
Remarks
When you append characters or strings to a StringBuilder
object, its internal buffer is resized if necessary. The capacity of the StringBuilder
is the size of the internal buffer, in characters. The length of the StringBuilder
is the number of characters it currently contains.
If the length of the StringBuilder
exceeds its capacity, the capacity is increased. The new capacity is typically double the current capacity, but the exact resizing behavior can vary depending on the implementation.
Using StringBuilder
is generally recommended over repeated concatenation of String
objects, especially within loops, to improve performance and reduce memory pressure.
Examples
Example 1: Basic Usage
using System;
using System.Text;
public class Example
{
public static void Main(string[] args)
{
StringBuilder sb = new StringBuilder();
sb.Append("Hello");
sb.Append(", ");
sb.Append("World!");
sb.AppendLine(); // Add a newline
sb.Append("This is a test.");
Console.WriteLine(sb.ToString());
// Output:
// Hello, World!
// This is a test.
}
}
Example 2: Using Capacity
using System;
using System.Text;
public class Example
{
public static void Main(string[] args)
{
// Initialize with a capacity to avoid frequent reallocations
StringBuilder sb = new StringBuilder(100);
sb.Append("This string builder has an initial capacity of 100 characters.");
Console.WriteLine($"Current Length: {sb.Length}");
Console.WriteLine($"Current Capacity: {sb.Capacity}");
Console.WriteLine(sb.ToString());
}
}