C# StringBuilder Methods

About This Index
This page is the C# StringBuilder Methods index. Each row links to a full guide with syntax, parameters, return value, three examples, and FAQs. Every example uses small fixed text and numbers, and formats numbers and dates with CultureInfo.InvariantCulture where it matters, so the output you see is the output you get.
Learn why StringBuilder exists on the StringBuilder hub. Use this page when you need a specific API such as Append(), Insert(), or ToString(). For the read-only text type, see String Methods.
A normal string never changes: every + makes a brand-new string. A StringBuilder keeps one growing buffer, so adding text in a loop is cheap. Most methods return the same builder, so calls can be chained. Call ToString() once at the end to get the finished string.
How to Call a StringBuilder Method
Every method here is an instance method, so you call it on your builder variable:
using System.Text;
StringBuilder sb = new StringBuilder();
sb.Append("Hello"); // Hello
sb.Append(' ').Append("World"); // Hello World (chained)
sb.Insert(0, ">> "); // >> Hello World
sb.Replace("World", "C#"); // >> Hello C#
int chars = sb.Length; // 11
string text = sb.ToString(); // the finished string C# StringBuilder Methods Table
All method guides in this C# tutorial series (13 tutorials), grouped by purpose.
Add text at the end
4 methodsGrow the buffer: add any value, a formatted message, a joined list, or a whole line. Each call returns the same builder, so calls can be chained.
| Method | Description | Tutorial |
|---|---|---|
Append() | Add any value to the end of the builder and get the same builder back for chaining. | Try it |
AppendFormat() | Add a composite-format message like "{0} is {1}" with values filled into the placeholders. | Try it |
AppendJoin() | Add every item of a list or array to the builder with a separator between them. | Try it |
AppendLine() | Add optional text followed by a line break (Environment.NewLine). | Try it |
Edit in place
4 methodsChange the text that is already in the buffer: put text in the middle, cut a range out, swap one value for another, or empty everything.
| Method | Description | Tutorial |
|---|---|---|
Insert() | Put text or a value at any position; the characters after it move to the right. | Try it |
Remove() | Cut a range of characters out of the builder by start position and length. | Try it |
Replace() | Swap every occurrence of a string or char for another one, in the whole text or a range. | Try it |
Clear() | Remove all characters (Length becomes 0) so the builder can be reused; memory is kept. | Try it |
Read / copy out
3 methodsTurn the buffer into a normal string, copy some characters into an array or span, or walk the internal pieces without making a string.
| Method | Description | Tutorial |
|---|---|---|
ToString() | Turn the builder (or part of it) into a normal string you can print, store, or compare. | Try it |
CopyTo() | Copy a range of characters from the builder into a char array or a span. | Try it |
GetChunks() | Walk the builder's internal pieces of text as ReadOnlyMemory<char> chunks, without making one big string. | Try it |
Compare and capacity
2 methodsCheck whether two builders hold the same text, and reserve room before a large build.
| Method | Description | Tutorial |
|---|---|---|
Equals() | Check whether two builders (or a builder and a span) hold the same characters. | Try it |
EnsureCapacity() | Reserve room for at least a number of characters before a big build; returns the new capacity. | Try it |
Frequently Asked Questions
Start with Append()
Add your first pieces of text to a builder and see how one buffer grows instead of creating a new string each time.
12 people found this page helpful
