Export (0) Print
Expand All

String.GetEnumerator Method

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

Namespace: System
Assembly: mscorlib (in mscorlib.dll)

public:
CharEnumerator^ GetEnumerator ()
public CharEnumerator GetEnumerator ()
public function GetEnumerator () : CharEnumerator
Not applicable.

Return Value

A CharEnumerator object.

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.

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

// 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 Server 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

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0

XNA Framework

Supported in: 1.0

Community Additions

ADD
Show:
© 2014 Microsoft