Skip to main content
.NET Framework Class Library
String..::.GetEnumerator Method

Retrieves an object that can iterate through the individual characters in this string.

Namespace: System
Assembly: mscorlib (in mscorlib.dll)
Syntax
Public Function GetEnumerator As CharEnumerator
public CharEnumerator GetEnumerator()
public:
CharEnumerator^ GetEnumerator()
member GetEnumerator : unit -> CharEnumerator 

Return Value

Type: System..::.CharEnumerator
An enumerator object.
Remarks

This method is required by programming languages that support the IEnumerator interface to iterate through members of a collection. For example, the Microsoft Visual Basic and C# programming languages' foreach statement invokes this method to return a CharEnumerator object that can provide read-only access to the characters in this instance of String.

Examples

The following example uses the GetEnumerator method to display each System..::.Char in the input strings.


' 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
*/

Version Information

.NET Framework

Supported in: 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1
Platforms

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Microsoft is conducting an online survey to understand your opinion of the MSDN Web site. If you choose to participate, the online survey will be presented to you when you leave the MSDN Web site.

Would you like to participate?