次の方法で共有


String.GetEnumerator メソッド

このインスタンスの個別の文字を反復処理するオブジェクトを取得します。

Public Function GetEnumerator() As CharEnumerator
[C#]
public CharEnumerator GetEnumerator();
[C++]
public: CharEnumerator* GetEnumerator();
[JScript]
public function GetEnumerator() : CharEnumerator;

戻り値

CharEnumerator オブジェクト。

解説

IEnumerator インターフェイスでのコレクションのメンバの反復処理をサポートするプログラミング言語では、このメソッドが必要です。たとえば、Microsoft Visual Basic および C# プログラミング言語の foreach ステートメントでは、この String のインスタンス内の文字への読み取り専用アクセスを実現する CharEnumerator オブジェクトを返すために、このメソッドが呼び出されます。

使用例

[Visual Basic, C#, C++] GetEnumerator メソッドを使用して、入力文字列の各 System.Char を表示する例を次に示します。

 
' Example for the String.GetEnumerator( ) method.
Imports System
Imports System.Collections
Imports Microsoft.VisualBasic

Module GetEnumerator
   
    Sub Main()
        Console.WriteLine( _
            "This example of String.GetEnumerator( ) " & _
            "generates the following output.")

        EnumerateAndDisplay("Test Case")
        EnumerateAndDisplay("Has" & vbTab & "two" & vbTab & "tabs")
        EnumerateAndDisplay("Two" & vbLf & "new" & vbLf & "lines")
    End Sub 'Main
       
    Sub EnumerateAndDisplay(Operand As String)

        Console.WriteLine( _
            vbCrLf & "The characters in the string ""{0}"" are:", Operand)
          
        Dim OperandEnum As IEnumerator = Operand.GetEnumerator()
        Dim CharCount As Integer = 0
          
        While OperandEnum.MoveNext()
            CharCount += 1
            Console.Write(" ""{0}"" ", OperandEnum.Current)
        End While

        Console.WriteLine(vbCrLf & " Character count: {0}", CharCount)

    End Sub 'EnumerateAndDisplay
End Module 'GetEnumerator

' This example of String.GetEnumerator( ) generates the following output.
' 
' The characters in the string "Test Case" are:
'  "T"  "e"  "s"  "t"  " "  "C"  "a"  "s"  "e"
'  Character count: 9
' 
' The characters in the string "Has       two     tabs" are:
'  "H"  "a"  "s"  "       "  "t"  "w"  "o"  "     "  "t"  "a"  "b"  "s"
'  Character count: 12
' 
' The characters in the string "Two
' new
' lines" are:
'  "T"  "w"  "o"  "
' "  "n"  "e"  "w"  "
' "  "l"  "i"  "n"  "e"  "s"
'  Character count: 13

[C#] 
// Example for the String.GetEnumerator( ) method.
using System;
using System.Collections;

class GetEnumerator 
{
    public static void Main() 
    {
        Console.WriteLine( 
            "This example of String.GetEnumerator( ) " +
            "generates the following output." );

        EnumerateAndDisplay( "Test Case" );
        EnumerateAndDisplay( "Has\ttwo\ttabs" );
        EnumerateAndDisplay( "Two\nnew\nlines" );
    }

    static void EnumerateAndDisplay( String Operand )
    {
        Console.WriteLine( 
            "\nThe characters in the string \"{0}\" are:",
            Operand );

        IEnumerator OperandEnum = Operand.GetEnumerator( );
        int         CharCount = 0;

        while( OperandEnum.MoveNext( ) )
        {
            CharCount++;
            Console.Write( " '{0}' ", OperandEnum.Current );
        }
        Console.WriteLine( "\n Character count: {0}", CharCount );
    }
}

/*
This example of String.GetEnumerator( ) generates the following output.

The characters in the string "Test Case" are:
 'T'  'e'  's'  't'  ' '  'C'  'a'  's'  'e'
 Character count: 9

The characters in the string "Has       two     tabs" are:
 'H'  'a'  's'  '       '  't'  'w'  'o'  '     '  't'  'a'  'b'  's'
 Character count: 12

The characters in the string "Two
new
lines" are:
 'T'  'w'  'o'  '
'  'n'  'e'  'w'  '
'  'l'  'i'  'n'  'e'  's'
 Character count: 13
*/

[C++] 
// Example for the String::GetEnumerator( ) method.
#using <mscorlib.dll>
using namespace System;
using namespace System::Collections;

void EnumerateAndDisplay( String* Operand )
{
    Console::WriteLine( 
        S"\nThe characters in the string \"{0}\" are:",
        Operand );

    IEnumerator* OperandEnum = Operand->GetEnumerator( );
    int          CharCount = 0;

    while( OperandEnum->MoveNext( ) )
    {
        CharCount++;
        Console::Write( S" '{0}' ", OperandEnum->Current );
    }
    Console::WriteLine( S"\n Character count: {0}", __box( CharCount ) );
}

void main() 
{
    Console::WriteLine( 
        S"This example of String::GetEnumerator( ) " 
        S"generates the following output." );

    EnumerateAndDisplay( S"Test Case" );
    EnumerateAndDisplay( S"Has\ttwo\ttabs" );
    EnumerateAndDisplay( S"Two\nnew\nlines" );
}

/*
This example of String::GetEnumerator( ) generates the following output.

The characters in the string "Test Case" are:
 'T'  'e'  's'  't'  ' '  'C'  'a'  's'  'e'
 Character count: 9

The characters in the string "Has       two     tabs" are:
 'H'  'a'  's'  '       '  't'  'w'  'o'  '     '  't'  'a'  'b'  's'
 Character count: 12

The characters in the string "Two
new
lines" are:
 'T'  'w'  'o'  '
'  'n'  'e'  'w'  '
'  'l'  'i'  'n'  'e'  's'
 Character count: 13
*/

[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 ファミリ, Common Language Infrastructure (CLI) Standard

参照

String クラス | String メンバ | System 名前空間 | IEnumerator | IEnumerable | Chars