StringBuilder.Length 属性
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
获取或设置当前 StringBuilder 对象的长度。
public:
property int Length { int get(); void set(int value); };
public int Length { get; set; }
member this.Length : int with get, set
Public Property Length As Integer
属性值
此实例的长度。
例外
为集运算指定的值小于零或大于 MaxCapacity。
示例
以下示例演示 属性 Length 。
using namespace System;
using namespace System::Text;
int main()
{
StringBuilder^ sb1 = gcnew StringBuilder( "abc" );
StringBuilder^ sb2 = gcnew StringBuilder( "abc",16 );
Console::WriteLine();
Console::WriteLine( "a1) sb1->Length = {0}, sb1->Capacity = {1}", sb1->Length, sb1->Capacity );
Console::WriteLine( "a2) sb2->Length = {0}, sb2->Capacity = {1}", sb2->Length, sb2->Capacity );
Console::WriteLine( "a3) sb1 = \"{0}\", sb2 = \"{1}\"", sb1, sb2 );
Console::WriteLine( "a4) sb1 equals sb2: {0}", sb1->Equals( sb2 ) );
Console::WriteLine();
Console::WriteLine( "Ensure sb1 has a capacity of at least 50 characters." );
sb1->EnsureCapacity( 50 );
Console::WriteLine();
Console::WriteLine( "b1) sb1->Length = {0}, sb1->Capacity = {1}", sb1->Length, sb1->Capacity );
Console::WriteLine( "b2) sb2->Length = {0}, sb2->Capacity = {1}", sb2->Length, sb2->Capacity );
Console::WriteLine( "b3) sb1 = \"{0}\", sb2 = \"{1}\"", sb1, sb2 );
Console::WriteLine( "b4) sb1 equals sb2: {0}", sb1->Equals( sb2 ) );
Console::WriteLine();
Console::WriteLine( "Set the length of sb1 to zero." );
Console::WriteLine( "Set the capacity of sb2 to 51 characters." );
sb1->Length = 0;
sb2->Capacity = 51;
Console::WriteLine();
Console::WriteLine( "c1) sb1->Length = {0}, sb1->Capacity = {1}", sb1->Length, sb1->Capacity );
Console::WriteLine( "c2) sb2->Length = {0}, sb2->Capacity = {1}", sb2->Length, sb2->Capacity );
Console::WriteLine( "c3) sb1 = \"{0}\", sb2 = \"{1}\"", sb1, sb2 );
Console::WriteLine( "c4) sb1 equals sb2: {0}", sb1->Equals( sb2 ) );
}
/*
The example displays the following output:
a1) sb1->Length = 3, sb1->Capacity = 16
a2) sb2->Length = 3, sb2->Capacity = 16
a3) sb1 = "abc", sb2 = "abc"
a4) sb1 equals sb2: True
Ensure sb1 has a capacity of at least 50 characters.
b1) sb1->Length = 3, sb1->Capacity = 50
b2) sb2->Length = 3, sb2->Capacity = 16
b3) sb1 = "abc", sb2 = "abc"
b4) sb1 equals sb2: False
Set the length of sb1 to zero.
Set the capacity of sb2 to 51 characters.
c1) sb1->Length = 0, sb1->Capacity = 50
c2) sb2->Length = 3, sb2->Capacity = 51
c3) sb1 = "", sb2 = "abc"
c4) sb1 equals sb2: False
*/
using System;
using System.Text;
class Sample
{
public static void Main()
{
StringBuilder sb1 = new StringBuilder("abc");
StringBuilder sb2 = new StringBuilder("abc", 16);
Console.WriteLine();
Console.WriteLine("a1) sb1.Length = {0}, sb1.Capacity = {1}", sb1.Length, sb1.Capacity);
Console.WriteLine("a2) sb2.Length = {0}, sb2.Capacity = {1}", sb2.Length, sb2.Capacity);
Console.WriteLine("a3) sb1.ToString() = \"{0}\", sb2.ToString() = \"{1}\"",
sb1.ToString(), sb2.ToString());
Console.WriteLine("a4) sb1 equals sb2: {0}", sb1.Equals(sb2));
Console.WriteLine();
Console.WriteLine("Ensure sb1 has a capacity of at least 50 characters.");
sb1.EnsureCapacity(50);
Console.WriteLine();
Console.WriteLine("b1) sb1.Length = {0}, sb1.Capacity = {1}", sb1.Length, sb1.Capacity);
Console.WriteLine("b2) sb2.Length = {0}, sb2.Capacity = {1}", sb2.Length, sb2.Capacity);
Console.WriteLine("b3) sb1.ToString() = \"{0}\", sb2.ToString() = \"{1}\"",
sb1.ToString(), sb2.ToString());
Console.WriteLine("b4) sb1 equals sb2: {0}", sb1.Equals(sb2));
Console.WriteLine();
Console.WriteLine("Set the length of sb1 to zero.");
Console.WriteLine("Set the capacity of sb2 to 51 characters.");
sb1.Length = 0;
sb2.Capacity = 51;
Console.WriteLine();
Console.WriteLine("c1) sb1.Length = {0}, sb1.Capacity = {1}", sb1.Length, sb1.Capacity);
Console.WriteLine("c2) sb2.Length = {0}, sb2.Capacity = {1}", sb2.Length, sb2.Capacity);
Console.WriteLine("c3) sb1.ToString() = \"{0}\", sb2.ToString() = \"{1}\"",
sb1.ToString(), sb2.ToString());
Console.WriteLine("c4) sb1 equals sb2: {0}", sb1.Equals(sb2));
}
}
/*
The example displays the following output:
a1) sb1.Length = 3, sb1.Capacity = 16
a2) sb2.Length = 3, sb2.Capacity = 16
a3) sb1.ToString() = "abc", sb2.ToString() = "abc"
a4) sb1 equals sb2: True
Ensure sb1 has a capacity of at least 50 characters.
b1) sb1.Length = 3, sb1.Capacity = 50
b2) sb2.Length = 3, sb2.Capacity = 16
b3) sb1.ToString() = "abc", sb2.ToString() = "abc"
b4) sb1 equals sb2: False
Set the length of sb1 to zero.
Set the capacity of sb2 to 51 characters.
c1) sb1.Length = 0, sb1.Capacity = 50
c2) sb2.Length = 3, sb2.Capacity = 51
c3) sb1.ToString() = "", sb2.ToString() = "abc"
c4) sb1 equals sb2: False
*/
open System.Text
let sb1 = StringBuilder "abc"
let sb2 = StringBuilder("abc", 16)
printfn $"a1) sb1.Length = {sb1.Length}, sb1.Capacity = {sb1.Capacity}"
printfn $"a2) sb2.Length = {sb2.Length}, sb2.Capacity = {sb2.Capacity}"
printfn $"a3) sb1.ToString() = \"{sb1}\", sb2.ToString() = \"{sb2}\""
printfn $"a4) sb1 equals sb2: {sb1.Equals sb2}"
printfn "\nEnsure sb1 has a capacity of at least 50 characters."
sb1.EnsureCapacity 50 |> ignore
printfn $"\nb1) sb1.Length = {sb1.Length}, sb1.Capacity = {sb1.Capacity}"
printfn $"b2) sb2.Length = {sb2.Length}, sb2.Capacity = {sb2.Capacity}"
printfn $"b3) sb1.ToString() = \"{sb1}\", sb2.ToString() = \"{sb2}\""
printfn $"b4) sb1 equals sb2: {sb1.Equals sb2}"
printfn "\nSet the length of sb1 to zero."
printfn "Set the capacity of sb2 to 51 characters."
sb1.Length <- 0
sb2.Capacity <- 51
printfn $"\nc1) sb1.Length = {sb1.Length}, sb1.Capacity = {sb1.Capacity}"
printfn $"c2) sb2.Length = {sb2.Length}, sb2.Capacity = {sb2.Capacity}"
printfn $"c3) sb1.ToString() = \"{sb1}\", sb2.ToString() = \"{sb2}\""
printfn $"c4) sb1 equals sb2: {sb1.Equals sb2}"
// The example displays the following output:
// a1) sb1.Length = 3, sb1.Capacity = 16
// a2) sb2.Length = 3, sb2.Capacity = 16
// a3) sb1.ToString() = "abc", sb2.ToString() = "abc"
// a4) sb1 equals sb2: True
//
// Ensure sb1 has a capacity of at least 50 characters.
//
// b1) sb1.Length = 3, sb1.Capacity = 50
// b2) sb2.Length = 3, sb2.Capacity = 16
// b3) sb1.ToString() = "abc", sb2.ToString() = "abc"
// b4) sb1 equals sb2: False
//
// Set the length of sb1 to zero.
// Set the capacity of sb2 to 51 characters.
//
// c1) sb1.Length = 0, sb1.Capacity = 50
// c2) sb2.Length = 3, sb2.Capacity = 51
// c3) sb1.ToString() = "", sb2.ToString() = "abc"
// c4) sb1 equals sb2: False
Imports System.Text
Class Sample
Public Shared Sub Main()
Dim sb1 As New StringBuilder("abc")
Dim sb2 As New StringBuilder("abc", 16)
Console.WriteLine()
Console.WriteLine("a1) sb1.Length = {0}, sb1.Capacity = {1}", sb1.Length, sb1.Capacity)
Console.WriteLine("a2) sb2.Length = {0}, sb2.Capacity = {1}", sb2.Length, sb2.Capacity)
Console.WriteLine("a3) sb1.ToString() = ""{0}"", sb2.ToString() = ""{1}""", _
sb1.ToString(), sb2.ToString())
Console.WriteLine("a4) sb1 equals sb2: {0}", sb1.Equals(sb2))
Console.WriteLine()
Console.WriteLine("Ensure sb1 has a capacity of at least 50 characters.")
sb1.EnsureCapacity(50)
Console.WriteLine()
Console.WriteLine("b1) sb1.Length = {0}, sb1.Capacity = {1}", sb1.Length, sb1.Capacity)
Console.WriteLine("b2) sb2.Length = {0}, sb2.Capacity = {1}", sb2.Length, sb2.Capacity)
Console.WriteLine("b3) sb1.ToString() = ""{0}"", sb2.ToString() = ""{1}""", _
sb1.ToString(), sb2.ToString())
Console.WriteLine("b4) sb1 equals sb2: {0}", sb1.Equals(sb2))
Console.WriteLine()
Console.WriteLine("Set the length of sb1 to zero.")
Console.WriteLine("Set the capacity of sb2 to 51 characters.")
sb1.Length = 0
sb2.Capacity = 51
Console.WriteLine()
Console.WriteLine("c1) sb1.Length = {0}, sb1.Capacity = {1}", sb1.Length, sb1.Capacity)
Console.WriteLine("c2) sb2.Length = {0}, sb2.Capacity = {1}", sb2.Length, sb2.Capacity)
Console.WriteLine("c3) sb1.ToString() = ""{0}"", sb2.ToString() = ""{1}""", _
sb1.ToString(), sb2.ToString())
Console.WriteLine("c4) sb1 equals sb2: {0}", sb1.Equals(sb2))
End Sub
End Class
'The example displays the following output:
' a1) sb1.Length = 3, sb1.Capacity = 16
' a2) sb2.Length = 3, sb2.Capacity = 16
' a3) sb1.ToString() = "abc", sb2.ToString() = "abc"
' a4) sb1 equals sb2: True
'
' Ensure sb1 has a capacity of at least 50 characters.
'
' b1) sb1.Length = 3, sb1.Capacity = 50
' b2) sb2.Length = 3, sb2.Capacity = 16
' b3) sb1.ToString() = "abc", sb2.ToString() = "abc"
' b4) sb1 equals sb2: False
'
' Set the length of sb1 to zero.
' Set the capacity of sb2 to 51 characters.
'
' c1) sb1.Length = 0, sb1.Capacity = 50
' c2) sb2.Length = 3, sb2.Capacity = 51
' c3) sb1.ToString() = "", sb2.ToString() = "abc"
' c4) sb1 equals sb2: False
注解
对象的长度 StringBuilder 由其对象数 Char 定义。
与 属性一 String.Length 样, Length 属性指示当前字符串对象的长度。 String.Length与 只读属性不同, Length 属性允许您修改存储在 StringBuilder 对象的字符串的长度。
如果指定的长度小于当前长度,则当前 StringBuilder 对象将被截断为指定的长度。 如果指定的长度大于当前长度,则当前 StringBuilder 对象的字符串值的末尾将填充 Unicode NULL 字符 (U+0000) 。
如果指定的长度大于当前容量, Capacity 则增大以使其大于或等于指定的长度。