Freigeben über


StringBuilder.Length-Eigenschaft

Ruft die Länge des aktuellen StringBuilder-Objekts ab oder legt diese fest.

Namespace: System.Text
Assembly: mscorlib (in mscorlib.dll)

Syntax

'Declaration
Public Property Length As Integer
'Usage
Dim instance As StringBuilder
Dim value As Integer

value = instance.Length

instance.Length = value
public int Length { get; set; }
public:
property int Length {
    int get ();
    void set (int value);
}
/** @property */
public int get_Length ()

/** @property */
public void set_Length (int value)
public function get Length () : int

public function set Length (value : int)

Eigenschaftenwert

Die Länge dieser Instanz.

Ausnahmen

Ausnahmetyp Bedingung

ArgumentOutOfRangeException

Der für einen set-Vorgang angegebene Wert ist kleiner als 0 (null) oder größer als MaxCapacity.

Hinweise

Wenn die angegebene Länge kleiner als die aktuelle Länge ist, wird das aktuelle StringBuilder-Objekt auf die angegebene Länge abgeschnitten. Wenn die angegebene Länge größer als die aktuelle Länge ist, wird das Ende des Zeichenfolgenwerts des aktuellen StringBuilder-Objekts mit dem Unicode-NULL-Zeichen (U+0000) aufgefüllt.

Wenn die angegebene Länge größer als die aktuelle Kapazität ist, wird Capacity auf die angegebene Länge festgelegt.

Beispiel

Im folgenden Codebeispiel wird die Length-Eigenschaft veranschaulicht.

Imports System
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 'Main
End Class 'Sample
'
'This example produces the following results:
'
'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 = 51
'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 = 51
'c2) sb2.Length = 3, sb2.Capacity = 51
'c3) sb1.ToString() = "", sb2.ToString() = "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));
    }
}
/*
This example produces the following results:

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 = 51
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 = 51
c2) sb2.Length = 3, sb2.Capacity = 51
c3) sb1.ToString() = "", sb2.ToString() = "abc"
c4) sb1 equals sb2: False
*/
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 ) );
}

/*
This example produces the following results:

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 = 51
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 = 51
c2) sb2.Length = 3, sb2.Capacity = 51
c3) sb1.ToString() = "", sb2.ToString() = "abc"
c4) sb1 equals sb2: False
*/
// This example demonstrates StringBuilder.EnsureCapacity
//                           StringBuilder.Capacity
//                           StringBuilder.Length
//                           StringBuilder.Equals
import System.*;
import System.Text.*;

class Sample
{
    public static void main(String[] args)
    {
        StringBuilder sb1 = new StringBuilder("abc");
        StringBuilder sb2 = new StringBuilder("abc", 16);

        Console.WriteLine();
        Console.WriteLine("a1) sb1.get_Length() = {0}, " 
            + "sb1.get_Capacity() = {1}", 
            System.Convert.ToString(sb1.get_Length()), 
            System.Convert.ToString(sb1.get_Capacity()));
        Console.WriteLine("a2) sb2.get_Length() = {0}, " 
            + "sb2.get_Capacity() = {1}", 
            System.Convert.ToString(sb2.get_Length()), 
            System.Convert.ToString(sb2.get_Capacity()));
        Console.WriteLine("a3) sb1.ToString() = \"{0}\", " 
            + "sb2.ToString() = \"{1}\"", sb1.ToString(), sb2.ToString());
        Console.WriteLine("a4) sb1 equals sb2: {0}", 
            System.Convert.ToString(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.get_Length() = {0}, " 
            + "sb1.get_Capacity() = {1}", 
            System.Convert.ToString(sb1.get_Length()), 
            System.Convert.ToString(sb1.get_Capacity()));
        Console.WriteLine("b2) sb2.get_Length() = {0}, " 
            + "sb2.get_Capacity() = {1}", 
            System.Convert.ToString(sb2.get_Length()), 
            System.Convert.ToString(sb2.get_Capacity()));
        Console.WriteLine("b3) sb1.ToString() = \"{0}\", " 
            + "sb2.ToString() = \"{1}\"", sb1.ToString(), sb2.ToString());
        Console.WriteLine("b4) sb1 equals sb2: {0}", 
            System.Convert.ToString(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.set_Length(0);
        sb2.set_Capacity(51);

        Console.WriteLine();
        Console.WriteLine("c1) sb1.get_Length() = {0}, "
            + "sb1.get_Capacity() = {1}", 
            System.Convert.ToString(sb1.get_Length()), 
            System.Convert.ToString(sb1.get_Capacity()));
        Console.WriteLine("c2) sb2.get_Length() = {0}, " 
            + "sb2.get_Capacity() = {1}", 
            System.Convert.ToString(sb2.get_Length()), 
            System.Convert.ToString(sb2.get_Capacity()));
        Console.WriteLine("c3) sb1.ToString() = \"{0}\", " 
            + "sb2.ToString() = \"{1}\"", sb1.ToString(), sb2.ToString());
        Console.WriteLine("c4) sb1 equals sb2: {0}", 
            System.Convert.ToString(sb1.Equals(sb2)));
    } //main
} //Sample
/*
This example produces the following results:

a1) sb1.get_Length() = 3, sb1.get_Capacity() = 16
a2) sb2.get_Length() = 3, sb2.get_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.get_Length() = 3, sb1.get_Capacity() = 50
b2) sb2.get_Length() = 3, sb2.get_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.get_Length() = 0, sb1.get_Capacity() = 50
c2) sb2.get_Length() = 3, sb2.get_Capacity() = 51
c3) sb1.ToString() = "", sb2.ToString() = "abc"
c4) sb1 equals sb2: False
*/

Plattformen

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile für Pocket PC, Windows Mobile für Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

.NET Framework unterstützt nicht alle Versionen sämtlicher Plattformen. Eine Liste der unterstützten Versionen finden Sie unter Systemanforderungen.

Versionsinformationen

.NET Framework

Unterstützt in: 2.0, 1.1, 1.0

.NET Compact Framework

Unterstützt in: 2.0, 1.0

Siehe auch

Referenz

StringBuilder-Klasse
StringBuilder-Member
System.Text-Namespace