Mega Code Archive

 
Categories / C# Book / 02 Essential Types
 

0254 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("rntsoft.com"); Console.WriteLine(sb); sb.Length = 0; Console.WriteLine("sb:"+sb); } } The output: rntsoft.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("rntsoft.com"); Console.WriteLine(sb); sb = new StringBuilder(); Console.WriteLine("sb:"+sb); } } The output: rntsoft.com sb: