Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

String.GetEnumerator Method

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

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

'Declaration
Public Function GetEnumerator As CharEnumerator

Return Value

Type: System.CharEnumerator
An enumerator object.

This method enables you to iterate the individual characters in a string. For example, the Visual Basic For Each and 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.

Module Example
   Sub Main()
      EnumerateAndDisplay("Test Case")
      EnumerateAndDisplay("This is a sentence.")
      EnumerateAndDisplay("Has" & vbTab & "two" & vbTab & "tabs")
      EnumerateAndDisplay("Two" & vbLf & "new" & vbLf & "lines")
   End Sub  

   Sub EnumerateAndDisplay(phrase As String)
      Console.WriteLine("The characters in the string ""{0}"" are:", phrase)

      Dim charCount As Integer = 0
      Dim controlChars As Integer = 0
      Dim alphanumeric As Integer = 0
      Dim punctuation As Integer = 0

      For Each ch In phrase          
         Console.Write("'{0}' ", If(Not Char.IsControl(ch), ch, 
                                     "0x" + Convert.ToUInt16(ch).ToString("X4")))
         If Char.IsLetterOrDigit(ch) Then 
            alphanumeric += 1
         Else If Char.IsControl(ch) Then 
            controlChars += 1
         Else If Char.IsPunctuation(ch) Then 
            punctuation += 1             
         End If
         CharCount += 1
      Next

      Console.WriteLine()
      Console.WriteLine("   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}", controlChars)
      Console.WriteLine()
    End Sub  
End Module  
' This 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

Supported in: 4.5.2, 4.5.1, 4.5, 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Show:
© 2014 Microsoft