共用方式為


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

傳回

StringBuilder

Length 為 0 (零) 的物件。

範例

下列範例會具現化 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)
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 是方便的方法,相當於將 Length 目前實例的屬性設定為 0 (零) 。

適用於