GetEnumerator Method

String::GetEnumerator Method ()


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

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

CharEnumerator^ GetEnumerator()

Return Value

Type: System::CharEnumerator^

An enumerator object.


Rather than calling the GetEnumerator method to retrieve a CharEnumerator object that you then use to enumerate a string, you should instead use your language's iteration construct ( in C#, in C++/CLR, and in Visual Basic).

This method enables you to iterate the individual characters in a string. For example, the Visual Basic For Eachand C# foreach statements invoke this method to return a CharEnumerator object that can provide read-only access to the characters in this string instance.

The following example iterates the characters in several strings and displays information about their individual characters. It uses the language iteration construct rather than a call to the GetEnumerator method.

using namespace System;

void EnumerateAndDisplay(String^ phrase)
   Console::WriteLine("The characters in the string \"{0}\" are:", 

   int CharCount = 0;
   int controlChars = 0;
   int alphanumeric = 0;
   int punctuation = 0;

   for each (Char ch in phrase) {
      Console::Write("'{0}' ", (! Char::IsControl(ch)) ? ch.ToString() : 
                                "0x" + Convert::ToUInt16(ch).ToString("X4"));
      if (Char::IsLetterOrDigit(ch)) 
      else if (Char::IsControl(ch)) 
      else if (Char::IsPunctuation(ch)) 

   Console::WriteLine("\n   Total characters:        {0,3}", CharCount);
   Console::WriteLine("   Alphanumeric characters: {0,3}", alphanumeric);
   Console::WriteLine("   Punctuation characters:  {0,3}", punctuation);
   Console::WriteLine("   Control Characters:      {0,3}\n", controlChars);

int main()
   EnumerateAndDisplay("Test Case");
   EnumerateAndDisplay("This is a sentence.");
// The example displays the following output:
//    The characters in the string "Test Case" are:
//    'T' 'e' 's' 't' ' ' 'C' 'a' 's' 'e'
//       Total characters:          9
//       Alphanumeric characters:   8
//       Punctuation characters:    0
//       Control Characters:        0
//    The characters in the string "This is a sentence." are:
//    'T' 'h' 'i' 's' ' ' 'i' 's' ' ' 'a' ' ' 's' 'e' 'n' 't' 'e' 'n' 'c' 'e' '.'
//       Total characters:         19
//       Alphanumeric characters:  15
//       Punctuation characters:    1
//       Control Characters:        0
//    The characters in the string "Has       two     tabs" are:
//    'H' 'a' 's' '0x0009' 't' 'w' 'o' '0x0009' 't' 'a' 'b' 's'
//       Total characters:         12
//       Alphanumeric characters:  10
//       Punctuation characters:    0
//       Control Characters:        2
//    The characters in the string "Two
//    new
//    lines" are:
//    'T' 'w' 'o' '0x000A' 'n' 'e' 'w' '0x000A' 'l' 'i' 'n' 'e' 's'
//       Total characters:         13
//       Alphanumeric characters:  11
//       Punctuation characters:    0
//       Control Characters:        2

.NET Framework
Available since 1.1
Return to top
© 2015 Microsoft