Clear a StringBuilder in C#

The Clear() method in C# is used to remove all characters from a StringBuilder object, effectively resetting it to an empty state. This method is efficient as it doesn't create a new object but simply resets the internal character buffer.

Syntax

Following is the syntax for the Clear() method −

stringBuilder.Clear();

Return Value

The Clear() method returns a reference to the same StringBuilder instance with all characters removed. This allows for method chaining.

Using StringBuilder Clear() Method

Example

The following example demonstrates how to clear a StringBuilder and check its length before and after clearing −

using System;
using System.Text;

public class Demo {
   public static void Main() {
      // string array
      string[] myStr = { "One", "Two", "Three", "Four" };
      StringBuilder str = new StringBuilder("We will print now...").AppendLine();

      // foreach loop to append elements
      foreach (string item in myStr) {
         str.Append(item).AppendLine();
      }
      Console.WriteLine(str.ToString());
      int len = str.Length;
      Console.WriteLine("Length: " + len);

      // clearing
      str.Clear();
      int len2 = str.Length;
      Console.WriteLine("Length after using Clear: " + len2);
   }
}

The output of the above code is −

We will print now...
One
Two
Three
Four

Length: 40
Length after using Clear: 0

Method Chaining with Clear()

Since Clear() returns the same StringBuilder instance, you can chain other methods after clearing −

Example

using System;
using System.Text;

public class Program {
   public static void Main() {
      StringBuilder sb = new StringBuilder("Initial content");
      Console.WriteLine("Before Clear: " + sb.ToString());
      
      // Clear and immediately append new content
      sb.Clear().Append("New content after clearing");
      Console.WriteLine("After Clear and Append: " + sb.ToString());
      
      // Multiple method chaining
      sb.Clear().Append("Hello").Append(" ").Append("World!");
      Console.WriteLine("Final content: " + sb.ToString());
   }
}

The output of the above code is −

Before Clear: Initial content
After Clear and Append: New content after clearing
Final content: Hello World!

Performance Comparison

Using Clear() is more efficient than creating a new StringBuilder instance −

Example

using System;
using System.Text;

public class Program {
   public static void Main() {
      StringBuilder sb = new StringBuilder("Sample text");
      Console.WriteLine("Original capacity: " + sb.Capacity);
      Console.WriteLine("Original content: " + sb.ToString());
      
      // Clear preserves capacity
      sb.Clear();
      Console.WriteLine("After Clear - capacity: " + sb.Capacity);
      Console.WriteLine("After Clear - length: " + sb.Length);
      Console.WriteLine("After Clear - content: '" + sb.ToString() + "'");
   }
}

The output of the above code is −

Original capacity: 16
Original content: Sample text
After Clear - capacity: 16
After Clear - length: 0
After Clear - content: ''

Conclusion

The Clear() method efficiently removes all characters from a StringBuilder while preserving its internal capacity. This makes it ideal for reusing StringBuilder instances in loops or when you need to reset content without creating new objects.

Updated on: 2026-03-17T07:04:35+05:30

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements