Javatpoint Logo
Javatpoint Logo

How to remove all characters from StringBuilder in C#

In this article, we will discuss how to remove all characters from StringBuilder in C#. But before discussing how to remove all characters from StringBuilder in C#, we must know about the StringBuilder in C#.

What is the StringBuilder in C#?

StringBuilder is a string class that can be modified in the System.Text namespace. This class allows us to modify the contents of a String object directly, providing better performance than traditional methods in the System. A String object with one return is created in this class, while the changes made to the String remain unaltered. Use the new keyword with the default constructor to initialise an empty, temporarily stored StringBuilder.

For example, the new string "Hello World" occupies space on the heap. We can change the string by changing the first string, "Hello World", to "Hello World String". Instead of changing the original String at the same memory address, "From the Tutorial Teacher" produces a new String object on the heap. This behaviour degrades performance if the original string is modified multiple times by replacing, adding, deleting, or inserting new strings.

Syntax:

It has the following syntax:

  • It returns a StringBuilder object with a length of zero.
  • The clear() method is a convenient method for setting the Length property of the current instance to 0 (zero).
  • Calling the Clear method does not change the Capacity or MaxCapacity properties of the current instance.

Need of StringBuilder:

In C#, string class objects are immutable. If a user needs to perform repeated operations on a string, a StringBuilder is required. It provides an optimised way to handle repetitive multiple-string manipulation operations.

Example:

Let us take a C# program to implement the clear() method of StringBuilder class.

Output:

The input String is: Programming 
The total length of the string is -- 11
The String is:  
The total length of the string is  -- 0
The given String is: This is javatpoint 
The total length of the string is  -- 18

Explanation:

  • In this example, the program starts with a StringBuilder object named initialised obj with the string "Programming". The Console.WriteLine specifies the beginning of the string, including its length.
  • After that, the Clear() method is called on the obj instance and successfully empties the contents of the StringBuilder. Another WriteLine array is used to display the updated state of the string, indicating empty strings and lengths.
  • Next, the function adds the string "This is a javatpoint" to the StringBuilder using the Append() method. The last Console.WriteLine statement displays the modified string and its updated length.

Example 2:

Let us take another C# program to implement the clear() method of StringBuilder class.

Output:

The string entries are.
Apple
Mango
Banana
Guava

The length of the string Builder is: 50
The length of the string after using the clear method: 0

Explanation:

In this example, the program begins by prefixing the string array with the user name. The StringBuilder is used to generate a string by adding each function name to the loop. The program displays the string produced and its length. After that, it uses the Clear() method to clear the StringBuilder. Finally, StringBuilder determines its length again to confirm isolation, resulting in an empty string.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA




news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news