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(零)的对象。
示例
以下示例使用字符串实例化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(零)。