C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
Tip: Clear internally sets the Length of the StringBuilder to zero. It provides a "clearer" syntax for this operation.
C# program that demonstrates Clear method
using System;
using System.Text;
class Program
{
static void Main()
{
var builder = new StringBuilder();
for (int i = 0; i < 10; i++)
{
builder.Append(i);
}
Console.WriteLine("Before Clear(): {0}", builder.Length);
builder.Clear();
Console.WriteLine("After Clear(): {0}", builder.Length);
}
}
Output
Before Clear(): 10
After Clear(): 0
Note: The "return this" part means you can call another method chained after Clear() in the same statement.
ReturnThisTip: Other methods on the StringBuilder also return the StringBuilder reference. More examples of this pattern are available.