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 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