In C#, StringBuilder.Clear() removes all the characters from the builder, so Length becomes 0 and ToString() returns an empty string. It does the same as setting sb.Length = 0.
Think of wiping a whiteboard instead of buying a new one. The board (the memory, or Capacity) usually stays, so the next text can be written without asking for new memory. That makes Clear handy when one builder is reused for many lines in a loop. It returns the same builder, so you can write sb.Clear().Append("new text").
An instance method of StringBuilder (using System.Text;), called as sb.Clear(...). The builder is a mutable buffer that changes in place, unlike an immutable string. It is not thread-safe. Clear() exists in .NET Framework 4.0 and later and in every .NET Core and .NET version. On older frameworks, set sb.Length = 0.
Syntax
Call it with no arguments.
C#
StringBuilder sb = new StringBuilder("draft");
sb.Clear(); // Length is 0, text is ""
sb.Append("final"); // reuse the same builder
Call
What it does
sb.Clear()
Removes every character. Length becomes 0.
sb.Clear().Append("x")
Empty and refill in one statement.
sb.Length = 0
Same effect; works on frameworks older than .NET 4.0 too.
sb = new StringBuilder()
Makes a different, new builder. Other variables still point to the old one.
Clear is a cheap operation: it does not overwrite the characters, it just marks the buffer as empty. The capacity is generally kept, which is good for reuse. If a builder once held a huge text and you want that memory back, create a new builder instead (or set Capacity lower after clearing).
Library declarations
C#
public StringBuilder Clear();
Parameters
Parameter
Description
None
Clear() takes no parameters.
Return Value
Return Value
Description
StringBuilder
The same builder, now empty. Use it to chain the next Append.
C# Examples
Example 1 — Clear Text but Keep the Capacity
Fill a builder that has room for 100 characters, clear it, and compare Length and Capacity before and after.
C#
using System;
using System.Text;
class Program {
static void Main() {
StringBuilder sb = new StringBuilder(100);
sb.Append("Temporary text");
Console.WriteLine(sb.Length + " / " + sb.Capacity);
sb.Clear();
Console.WriteLine(sb.Length + " / " + sb.Capacity);
Console.WriteLine(sb.ToString() == "");
}
}
Output
14 / 100
0 / 100
True
How It Works
Before.Temporary text has 14 characters, and the builder was created with room for 100, so the first line is 14 / 100.
After.Clear() drops the length to 0 while the room for 100 characters stays, giving 0 / 100.
Empty text.ToString() now returns "", so comparing it with an empty string prints True.
Ready to reuse. The next 100 characters can be appended without the builder asking for more memory.
Example 2 — Reuse One Builder for Every Line
Clear one builder at the start of each pass and build a greeting for every name.
C#
using System;
using System.Text;
class Program {
static void Main() {
string[] names = { "Ann", "Bo", "Cy" };
StringBuilder line = new StringBuilder();
foreach (string name in names) {
line.Clear();
line.Append("Hi, ").Append(name).Append('!');
Console.WriteLine(line.ToString());
}
}
}
Output
Hi, Ann!
Hi, Bo!
Hi, Cy!
How It Works
One builder, many lines.line is created once, before the loop, instead of once per name.
Wipe first.line.Clear() at the top of each pass removes the previous greeting, so the texts never run together.
Refill. Three chained Append calls write Hi,, the name, and ! into the emptied buffer.
Without Clear. Leaving out the Clear() call would print Hi, Ann!Hi, Bo! on the second line, because the builder would keep growing.
Example 3 — Clear, Length = 0, and Shared References
Chain Clear with Append, empty another builder by setting Length, and see how clearing affects a second variable and an earlier string.
C#
using System;
using System.Text;
class Program {
static void Main() {
StringBuilder sb = new StringBuilder("old value");
sb.Clear().Append("fresh");
Console.WriteLine(sb.ToString());
StringBuilder other = new StringBuilder("abc");
other.Length = 0;
Console.WriteLine(other.Append("xyz").ToString());
StringBuilder alias = sb;
string saved = sb.ToString();
sb.Clear();
Console.WriteLine(alias.Length);
Console.WriteLine(saved);
}
}
Output
fresh
xyz
0
fresh
How It Works
Clear then append.Clear() returns the empty builder, so .Append("fresh") writes into it right away; old value is gone.
The Length way. Setting other.Length = 0 empties abc just like Clear(), and xyz is all that is left after the append.
One object, two names.alias points to the same builder as sb, so after sb.Clear() its length is 0 as well.
Strings are copies.saved was made by ToString() before the clear. Strings cannot change, so it still prints fresh.
Frequently Asked Questions
It removes all characters from the builder so Length becomes 0, and returns the same builder. It is the same as setting Length to 0.
Not usually. The reserved capacity is generally kept so the builder can be refilled quickly. Create a new StringBuilder if you want to release a very large buffer.
Clear is usually better: it reuses the same buffer, so fewer objects are created. A new builder per pass also works but makes more garbage.
.NET Framework 4.0 and later, and every version of .NET Core and .NET. On older frameworks use sb.Length = 0.
No. ToString returns a separate string, and strings never change. Only the builder itself is emptied.