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[] myStr = { "One", "Two", "Three", "Four" };
StringBuilder str = new StringBuilder("We will print now...").AppendLine();
// तत्व जोड़ने के लिए foreach लूप
foreach (string item in myStr) {
str.Append(item).AppendLine();
}
Console.WriteLine(str.ToString());
int len = str.Length;
Console.WriteLine("लंबाई: " + len);
// साफ़ करना
str.Clear();
int len2 = str.Length;
Console.WriteLine("Clear उपयोग के बाद लंबाई: " + len2);
Console.ReadLine();
}
}
आउटपुट
We will print now... One Two Three Four लंबाई: 40 Clear उपयोग के बाद लंबाई: 0