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. 理由は、1 つ以上で Unicode 文字を表す場合があります、Charします。The reason is that a Unicode character might be represented by more than one Char. 使用して、System.Globalization.StringInfoごとではなく各 Unicode 文字を使用するクラス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. 文字列には、1 つまたは複数の 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プロパティは 6 つのアルファベット文字と null 文字含まれていることを示します 7 を返します。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

適用対象

こちらもご覧ください