在C#中清除StringBuilder
要清除StringBuilder,请使用Clear()方法。
假设我们已经设置了以下StringBuilder-
string[] myStr = { "One", "Two", "Three", "Four" };
StringBuilder str = new StringBuilder("We will print now...").AppendLine();现在,使用该Clear()方法清除StringBuilder-
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("Length: "+len);
//清理
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
热门推荐
5 短祝福语简短暖心
10 结婚祝福语粤语大全简短
11 晚上祝福语女生文案简短
12 法语妈妈生日祝福语简短
13 药厂开工祝福语大全简短
14 蛋糕节日祝福语简短英文
15 跨年的生日祝福语简短
16 文案祝福语英文短句简短
17 在家聚餐婚礼祝福语简短
18 学生节祝福语大全简短