Clear a StringBuilder

To clear the content of a string builder, set its length to 0.


using System;
using System.Text;
class Sample
{
    public static void Main()
    {
        StringBuilder sb = new StringBuilder();

        sb.Append("java2s.com");

        Console.WriteLine(sb);

        sb.Length = 0;

        Console.WriteLine("sb:"+sb);
    }
}

The output:


java2s.com
sb:

Another way to clear a StringBuilder is to reinitialize the instance again.


using System;
using System.Text;
class Sample
{
    public static void Main()
    {
        StringBuilder sb = new StringBuilder();

        sb.Append("java2s.com");

        Console.WriteLine(sb);

        sb = new StringBuilder();

        Console.WriteLine("sb:"+sb);
    }
}

The output:


java2s.com
sb:
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.