String.Length String.Length String.Length String.Length Property

定義

取得目前 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

屬性值

目前字串中的字元數。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
*/
using System;

class Sample 
{
    public static void 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", "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 'Main
End Class 'Sample
'
'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 字元,而不是每個CharUse 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
}
using System;
using System.Text;

public class StringClassTest
{
   public static void Main()
   {
      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

適用於

另請參閱