String.Length 属性

定义

获取当前 String 对象中的字符数。Gets the number of characters in the current String object.

public:
 property int Length { int get(); };
public int Length { get; }
member this.Length : int
Public ReadOnly Property Length As Integer

属性值

Int32

当前字符串中字符的数量。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 此实例中的对象数,而不是 Unicode 字符数。The Length property returns the number of Char objects in this instance, not the number of Unicode characters. 原因是 Unicode 字符可能由多个表示 CharThe reason is that a Unicode character might be represented by more than one Char. 使用 System.Globalization.StringInfo 类来处理每个 Unicode 字符,而不是每个 Unicode 字符 CharUse the System.Globalization.StringInfo class to work with each Unicode character instead of each Char.

在某些语言(如 C 和 c + +)中,空字符表示字符串的末尾。In some languages, such as C and C++, a null character indicates the end of a string. 在 .NET 中,可以在字符串中嵌入空字符。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

适用于

另请参阅