String.Length Właściwość
Definicja
Ważne
Niektóre informacje odnoszą się do produktu w wersji wstępnej, który może zostać znacząco zmodyfikowany przed wydaniem. Firma Microsoft nie udziela żadnych gwarancji, jawnych lub domniemanych, w odniesieniu do informacji podanych w tym miejscu.
Pobiera liczbę znaków w bieżącym String obiekcie.
public:
property int Length { int get(); };
public int Length { get; }
member this.Length : int
Public ReadOnly Property Length As Integer
Wartość właściwości
Liczba znaków w bieżącym ciągu.
Przykłady
W poniższym przykładzie pokazano Length właściwość .
// Sample for String::Length
using namespace System;
int main()
{
String^ str = "abcdefg";
Console::WriteLine( "1) The length of '{0}' is {1}", str, str->Length );
Console::WriteLine( "2) The length of '{0}' is {1}", "xyz", ((String^)"xyz")->Length );
int length = str->Length;
Console::WriteLine( "1) The length of '{0}' is {1}", str, length );
}
/*
This example displays the following output:
1) The length of 'abcdefg' is 7
2) The length of 'xyz' is 3
3) The length of 'abcdefg' is 7
*/
string str = "abcdefg";
Console.WriteLine("1) The length of '{0}' is {1}", str, str.Length);
Console.WriteLine("2) The length of '{0}' is {1}", "xyz", "xyz".Length);
int length = str.Length;
Console.WriteLine("3) The length of '{0}' is {1}", str, length);
// This example displays the following output:
// 1) The length of 'abcdefg' is 7
// 2) The length of 'xyz' is 3
// 3) The length of 'abcdefg' is 7
Class Sample
Public Shared Sub Main()
Dim str As String = "abcdefg"
Console.WriteLine("1) The length of '{0}' is {1}", str, str.Length)
Console.WriteLine("2) The length of '{0}' is {1}", "xyz", "xyz".Length)
Dim length As Integer = str.Length
Console.WriteLine("1) The length of '{0}' is {1}", str, length)
End Sub
End Class
'
'This example displays the following output:
' 1) The length of 'abcdefg' is 7
' 2) The length of 'xyz' is 3
' 3) The length of 'abcdefg' is 7
Uwagi
Właściwość Length zwraca liczbę obiektów w tym Char wystąpieniu, a nie liczbę znaków Unicode. Przyczyną jest to, że znak Unicode może być reprezentowany przez więcej niż jeden Char znak . Użyj klasy System.Globalization.StringInfo , aby pracować z każdym znakiem Unicode, a nie z każdym znakiem Char .
W niektórych językach, takich jak C i C++, znak null wskazuje koniec ciągu. Na platformie .NET znak null może być osadzony w ciągu. Gdy ciąg zawiera jeden lub więcej znaków o wartości null, są one uwzględniane w długości całkowitego ciągu. Na przykład w poniższym ciągu podciągi "abc" i "def" są oddzielone znakiem null. Właściwość zwraca wartość 7, co oznacza, że zawiera sześć znaków alfabetycznych, a Length także znak null.
using namespace System;
using namespace System::Text;
void main()
{
String^ characters = "abc" + L'0' + "def";
Console::WriteLine(characters->Length); // Displays 7
}
string characters = "abc\u0000def";
Console.WriteLine(characters.Length); // Displays 7
Imports System.Text
Module Example
Public Sub Main()
Dim characters As String = "abc" + ChrW(0) + "def"
Console.WriteLine(characters.Length) ' Displays 7
End Sub
End Module