StringBuilder को साफ़ करने के लिए, Clear() विधि का उपयोग करें।
मान लें कि हमने निम्नलिखित StringBuilder सेट किया है -
string[] myStr = { "One", "Two", "Three", "Four" };
StringBuilder str = new StringBuilder("We will print now...").AppendLine(); अब, StringBuilder को साफ़ करने के लिए Clear() विधि का उपयोग करें -
str.Clear();
आइए देखें पूरा कोड -
उदाहरण
using System;
using System.Text;
public class Demo {
public static void Main() {
// string array
string[] myStr = { "One", "Two", "Three", "Four" };
StringBuilder str = new StringBuilder("We will print now...").AppendLine();
// foreach loop to append elements
foreach (string item in myStr) {
str.Append(item).AppendLine();
}
Console.WriteLine(str.ToString());
int len = str.Length;
Console.WriteLine("Length: "+len);
// clearing
str.Clear();
int len2 = str.Length;
Console.WriteLine("Length after using Clear: "+len2);
Console.ReadLine();
}
} आउटपुट
We will print now... One Two Three Four Length: 40 Length after using Clear: 0