다음을 통해 공유


StringBuilder.Clear 메서드

정의

현재 StringBuilder 인스턴스에서 모든 문자를 제거합니다.

public:
 System::Text::StringBuilder ^ Clear();
public System.Text.StringBuilder Clear();
member this.Clear : unit -> System.Text.StringBuilder
Public Function Clear () As StringBuilder

반환

0인 개체 Length 입니다.

예제

다음 예제에서는 문자열로 개체를 StringBuilder 인스턴스화하고 메서드를 Clear 호출한 다음 새 문자열을 추가합니다.

using System;
using System.Text;

public class Class1
{
   public static void Main()
   {
      StringBuilder sb = new StringBuilder("This is a string.");
      Console.WriteLine("{0} ({1} characters)", sb.ToString(), sb.Length);
      
      sb.Clear();
      Console.WriteLine("{0} ({1} characters)", sb.ToString(), sb.Length);

      sb.Append("This is a second string.");
      Console.WriteLine("{0} ({1} characters)", sb.ToString(), sb.Length);
   }
}
// The example displays the following output:
//       This is a string. (17 characters)
//        (0 characters)
//       This is a second string. (24 characters)
open System.Text

let sb = StringBuilder "This is a string."
printfn $"{sb} ({sb.Length} characters)"

sb.Clear() |> ignore
printfn $"{sb} ({sb.Length} characters)"

sb.Append "This is a second string." |> ignore
printfn $"{sb} ({sb.Length} characters)"

// The example displays the following output:
//       This is a string. (17 characters)
//        (0 characters)
//       This is a second string. (24 characters)
Imports System.Text

Module Example
   Public Sub Main()
      Dim sb As New StringBuilder("This is a string.")
      Console.WriteLine("{0} ({1} characters)", sb.ToString(), sb.Length)
      
      sb.Clear()
      Console.WriteLine("{0} ({1} characters)", sb.ToString(), sb.Length)

      sb.Append("This is a second string.")
      Console.WriteLine("{0} ({1} characters)", sb.ToString(), sb.Length)
   End Sub            
End Module
' The example displays the following output:
'       This is a string. (17 characters)
'        (0 characters)
'       This is a second string. (24 characters)

설명

Clear 는 현재 인스턴스의 속성을 0으로 설정하는 Length 것과 동일한 편리한 메서드입니다.

적용 대상