How to remove all characters from StringBuilder in C#
StringBuilder.Clear Method is used to remove all the characters from the current StringBuilder instance.
Syntax:
public System.Text.StringBuilder Clear ();
It returns an StringBuilder object whose length will be zero.
Note:
- Clear is a convenience method that is equivalent to setting the Length property of the current instance to 0 (zero).
- Calling the Clear method does not modify the current instance’s Capacity or MaxCapacity property.
Example:
using System;
using System.Text;
class GFG {
public static void Main(String[] args)
{
StringBuilder sb1 = new StringBuilder( "GeeksforGeeks" );
Console.WriteLine( "The String is: {0} \nTotal characters -- {1}" ,
sb1.ToString(), sb1.Length);
sb1.Clear();
Console.WriteLine( "The String is: {0} \nTotal characters -- {1}" ,
sb1.ToString(), sb1.Length);
sb1.Append( "This is C# Tutorials." );
Console.WriteLine( "The String is: {0} \nTotal characters -- {1}" ,
sb1.ToString(), sb1.Length);
}
}
|
Output:
The String is: GeeksforGeeks
Total characters -- 13
The String is:
Total characters -- 0
The String is: This is C# Tutorials.
Total characters -- 22
Reference:
Last Updated :
28 Jan, 2019
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...