C# StringBuilder Clear

Description

StringBuilder Clear Removes all characters from the current StringBuilder instance.

Syntax

StringBuilder.Clear has the following syntax.


public StringBuilder Clear()

Returns

StringBuilder.Clear method returns An object whose Length is 0 (zero).

Example

The following example instantiates a StringBuilder object with a string, calls the Clear method, and then appends a new string.


using System;//  w ww. ja  va2s  .c om
using System.Text;

public class Class1
{
   public static void Main()
   {
      StringBuilder sb = new StringBuilder("This is a string.");
      Console.WriteLine("{0} ({1} characters)", sb.ToString(), sb.Length);

      sb.Clear();
      Console.WriteLine("{0} ({1} characters)", sb.ToString(), sb.Length);

      sb.Append("This is a second string.");
      Console.WriteLine("{0} ({1} characters)", sb.ToString(), sb.Length);
   }
}

The code above generates the following result.





















Home »
  C# Tutorial »
    System.Text »




ASCIIEncoding
Encoding
EncodingInfo
StringBuilder
UnicodeEncoding
UTF8Encoding