String.Length Свойство
Определение
public:
property int Length { int get(); };
public int Length { get; }
member this.Length : int
Public ReadOnly Property Length As Integer
Значение свойства
Количество знаков в текущей строке.The number of characters in the current string.
Примеры
В следующем примере демонстрируется Length свойство.The following example demonstrates the Length property.
// 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
Комментарии
LengthСвойство возвращает количество Char объектов в данном экземпляре, а не число символов Юникода.The Length property returns the number of Char objects in this instance, not the number of Unicode characters. Причина заключается в том, что символ Юникода может быть представлен более чем одним Char .The reason is that a Unicode character might be represented by more than one Char. Используйте System.Globalization.StringInfo класс для работы с каждым символом Юникода, а не с каждым Char .Use the System.Globalization.StringInfo class to work with each Unicode character instead of each Char.
В некоторых языках, таких как C и C++, символ NULL обозначает конец строки.In some languages, such as C and C++, a null character indicates the end of a string. В .NET символ NULL может быть внедрен в строку.In .NET, a null character can be embedded in a string. Если строка содержит один или несколько символов NULL, они включаются в общую длину строки.When a string includes one or more null characters, they are included in the length of the total string. Например, в следующей строке подстроки "ABC" и "def" разделяются символом NULL.For example, in the following string, the substrings "abc" and "def" are separated by a null character. LengthСвойство возвращает значение 7, которое указывает, что включает в себя шесть алфавитных символов, а также символ null.The Length property returns 7, which indicates that it includes the six alphabetic characters as well as the null character.
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