StringBuilder.Clear 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
移除目前 StringBuilder 實例中的所有角色。
public:
System::Text::StringBuilder ^ Clear();
public System.Text.StringBuilder Clear();
member this.Clear : unit -> System.Text.StringBuilder
Public Function Clear () As StringBuilder
傳回
一個物件,其 Length 為0(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)
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 是一種便利方法,等同於將當前實例的性質設 Length 為0(0)。