Java
StringBuilder
Programming
Software Development
Coding Solutions

How can I clear or empty a StringBuilder?

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

To empty a .NET StringBuilder, the clearest solution is Clear(). It removes all characters by setting the length to zero while keeping the existing StringBuilder instance available for reuse.

The operation is simple, but there are still a few practical questions around readability, capacity, and whether you want to reuse the builder or allocate a new one. Those details matter in code that runs often.

Use Clear() for the Normal Case

The standard approach is:

csharp
1using System.Text;
2
3var sb = new StringBuilder("Initial content");
4sb.Clear();
5
6Console.WriteLine(sb.Length); // 0

Clear() is the most expressive choice because it says exactly what you mean. Anyone reading the code immediately understands that the existing builder is being emptied for reuse.

Length = 0 Does the Same Thing

Older code often uses:

csharp
1using System.Text;
2
3var sb = new StringBuilder("Initial content");
4sb.Length = 0;

This also empties the builder. In practice, Clear() and Length = 0 are equivalent for most use cases, but Clear() communicates intent more directly.

If you see either one in existing code, the behavior is the same: the characters are removed and the builder becomes empty.

Reuse Versus Reallocate

Sometimes developers write:

csharp
sb = new StringBuilder();

That works, but it creates a new instance instead of reusing the existing one. If the goal is simply to empty the builder and continue, Clear() is usually better because it avoids an unnecessary allocation.

Reinitializing can still make sense if you explicitly want a fresh builder with different construction settings, but it is not the normal answer to "how do I empty it?"

Capacity Does Not Automatically Shrink

One detail that surprises people is that clearing a StringBuilder does not automatically reduce its capacity:

csharp
1using System.Text;
2
3var sb = new StringBuilder();
4sb.Append('x', 1000);
5
6Console.WriteLine(sb.Capacity);
7sb.Clear();
8Console.WriteLine(sb.Capacity);

That is often a good thing, because it allows efficient reuse without repeated buffer growth. But if the builder expanded to a very large size and you no longer want to hold that memory, you may also want to adjust capacity or replace the builder with a new instance.

A Common Reuse Pattern

Reusing a builder inside a loop is a normal performance pattern:

csharp
1using System.Text;
2
3var sb = new StringBuilder();
4
5for (int i = 0; i < 3; i++)
6{
7    sb.Clear();
8    sb.Append("Run ");
9    sb.Append(i);
10    Console.WriteLine(sb.ToString());
11}

This avoids repeated allocation while keeping the code straightforward.

Common Pitfalls

The biggest mistake is recreating a new StringBuilder every time when the old one could simply be cleared and reused. In hot paths, that creates avoidable allocations.

Another common issue is forgetting that clearing the builder does not reduce capacity. If the builder once held a very large string, it may still keep a large buffer afterward.

Developers also sometimes use Length = 0 in new code without a strong reason. It works, but Clear() usually communicates intent better.

Finally, be careful not to confuse StringBuilder behavior across languages. This article is about the .NET StringBuilder, where Clear() is the idiomatic answer.

Summary

  • Use StringBuilder.Clear() to empty a .NET StringBuilder.
  • Setting Length = 0 has the same effect but is less explicit.
  • Reusing the same builder is usually better than allocating a new one just to clear content.
  • Clearing does not automatically shrink capacity.
  • If memory footprint matters after a very large append, consider adjusting capacity or replacing the builder deliberately.

Course illustration
Course illustration

All Rights Reserved.