다음을 통해 공유


String.GetEnumerator 메서드

이 문자열의 개별 문자에서 반복될 수 있는 개체를 검색합니다.

네임스페이스: System
어셈블리: mscorlib(mscorlib.dll)

구문

‘선언
Public Function GetEnumerator As CharEnumerator
‘사용 방법
Dim instance As String
Dim returnValue As CharEnumerator

returnValue = instance.GetEnumerator
public CharEnumerator GetEnumerator ()
public:
CharEnumerator^ GetEnumerator ()
public CharEnumerator GetEnumerator ()
public function GetEnumerator () : CharEnumerator

반환 값

CharEnumerator 개체입니다.

설명

이 메서드는 컬렉션 멤버에서 반복하도록 IEnumerator 인터페이스를 지원하는 프로그래밍 언어에 필요합니다. 예를 들어, Microsoft Visual Basic 및 C# 프로그래밍 언어에서 foreach 문은 이 메서드를 호출하여 String의 이 인스턴스에 포함된 문자에 대한 읽기 전용 액세스를 제공하는 CharEnumerator 개체를 반환합니다.

예제

다음 코드 예제에서는 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
// 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
*/
// Example for the String::GetEnumerator( ) method.
using namespace System;
using namespace System::Collections;
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 );
}

int main()
{
   Console::WriteLine( "This example of String::GetEnumerator( ) "
   "generates the following output." );
   EnumerateAndDisplay( "Test Case" );
   EnumerateAndDisplay( "Has\ttwo\ttabs" );
   EnumerateAndDisplay( "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
*/
// Example for the String.GetEnumerator( ) method.
import System.*;
import System.Collections.*;

class GetEnumerator
{
    public static void main(String[] args)
    {
        Console.WriteLine(("This example of String.GetEnumerator( ) "
            + "generates the following output."));
        EnumerateAndDisplay("Test Case");
        EnumerateAndDisplay("Has\ttwo\ttabs");
        EnumerateAndDisplay("Two\nnew\nlines");
    } //main

    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.get_Current());
        }
        Console.WriteLine("\n Character count: {0}", 
            String.valueOf(charCount));
    } //EnumerateAndDisplay
} //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
*/

플랫폼

Windows 98, Windows 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

.NET Framework에서 모든 플래폼의 모든 버전을 지원하지는 않습니다. 지원되는 버전의 목록은 시스템 요구 사항을 참조하십시오.

버전 정보

.NET Framework

2.0, 1.1, 1.0에서 지원

.NET Compact Framework

2.0에서 지원

참고 항목

참조

String 클래스
String 멤버
System 네임스페이스
IEnumerator
IEnumerable
Chars