Share via


String.Length プロパティ

このインスタンスの文字数を取得します。

Public ReadOnly Property Length As Integer
[C#]
public int Length {get;}
[C++]
public: __property int get_Length();
[JScript]
public function get Length() : int;

プロパティ値

このインスタンスの文字数。

解説

Length プロパティは、このインスタンス内の Char オブジェクトの数を返します。Unicode 文字の数ではありません。これは、1 つの Unicode 文字が複数の Char で表されることがあるためです。おのおのの Char ではなくおのおのの Unicode 文字を対象に処理を行うには、 System.Globalization.StringInfo クラスを使用します。

使用例

[Visual Basic, C#, C++] Length プロパティの例を次に示します。

 
' Sample for String.Length
Imports System

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)
   End Sub 'Main
End Class 'Sample
'
'This example produces the following results:
'1) The length of 'abcdefg' is 7
'2) The length of 'xyz' is 3
'

[C#] 
// Sample for String.Length
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);
    }
}
/*
This example produces the following results:
1) The length of 'abcdefg' is 7
2) The length of 'xyz' is 3
*/

[C++] 
// Sample for String::Length
#using <mscorlib.dll>

using namespace System;

int main() {
   String* str = S"abcdefg";
   Console::WriteLine(S"1) The length of '{0}' is {1}", str, __box( str->Length));
   Console::WriteLine(S"2) The length of '{0}' is {1}", S"xyz", __box( S"xyz"->Length));
}
/*
This example produces the following results:
1) The length of 'abcdefg' is 7
2) The length of 'xyz' is 3
*/

[JScript] JScript のサンプルはありません。Visual Basic、C#、および C++ のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン 言語のフィルタ をクリックします。

必要条件

プラットフォーム: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 ファミリ, .NET Compact Framework - Windows CE .NET, Common Language Infrastructure (CLI) Standard

参照

String クラス | String メンバ | System 名前空間 | Int32