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:
// C# program to demonstrate // the Clear() Method using System; using System.Text; class GFG { // Main Method public static void Main(String[] args) { // Creating a StringBuilder object StringBuilder sb1 = new StringBuilder("GeeksforGeeks"); // printing the length of "GeeksforGeeks" Console.WriteLine("The String is: {0} \nTotal characters -- {1}", sb1.ToString(), sb1.Length); // using the method sb1.Clear(); // printing the length of "GeeksforGeeks" Console.WriteLine("The String is: {0} \nTotal characters -- {1}", sb1.ToString(), sb1.Length); sb1.Append("This is C# Tutorials."); // printing the length of "GeeksforGeeks" Console.WriteLine("The String is: {0} \nTotal characters -- {1}", sb1.ToString(), sb1.Length); } } |
chevron_right
filter_none
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:
Recommended Posts:
- StringBuilder in C#
- C# | String vs StringBuilder
- How to create the StringBuilder in C#
- How to find the length of the StringBuilder in C#
- StringBuilder.Chars[] Property in C#
- StringBuilder.EnsureCapacity() Method in C#
- How to find the Capacity of a StringBuilder in C#
- C# | Creating StringBuilder having specified capacity
- How to find the MaxCapacity of a StringBuilder in C#
- StringBuilder.CopyTo Method in C#
- StringBuilder.ToString Method in C#
- C# | Check if two StringBuilder objects are Equal
- C# | Remove() Method
- C# | Remove all the strings from the StringCollection
- C# | Remove the first occurrence from the StringCollection
- C# | Remove from the specified index of a SortedList
- C# | Remove all elements from a SortedList
- C# | Remove entry with specified key from the StringDictionary
- C# | Remove the element with the specified key from a SortedList
- C# | Remove from the specified index of the StringCollection
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

