Export (0) Print
Expand All

CharEnumerator.MoveNext Method

Increments the internal index of the current CharEnumerator object to the next character of the enumerated string.

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

'Declaration
Public Function MoveNext As Boolean

Return Value

Type: System.Boolean
true if the index is successfully incremented and within the enumerated string; otherwise, false.

Implements

IEnumerator.MoveNext

The CharEnumerator class maintains an internal index to the enumerated string, and the MoveNext method increments the index by one. Call MoveNext after calling GetEnumerator or Reset to increment the current character position to the first character in the enumerated string. Check that the return value is true to determine that the current character position is valid.

If the index is already beyond the last character of the enumerated string, the index is not changed and false is returned.

Notice that if the enumerated string is empty (""), the state of the CharEnumerator is always invalid. This is because the internal index for the CharEnumerator is initially before the first character of the enumerated string and is therefore invalid. MoveNext logically sets the index after the last (nonexistent) character of the enumerated string which is also invalid.

The following example uses the CharEnumerator class to enumerate the individual characters in a string. It instantiates a CharEnumerator object by calling the String.GetEnumerator method, moves from one character to the next by calling the MoveNext method, and displays the current character by retrieving the value of the Current property.

Dim title As String = "A Tale of Two Cities" 
Dim chEnum As CharEnumerator = title.GetEnumerator()
Dim ctr As Integer = 1
Dim outputLine1, outputLine2, outputLine3 As String  

Do While chEnum.MoveNext()
   outputLine1 += CStr(iif(ctr < 10 Or ctr Mod 10 <> 0, "  ", CStr(ctr \ 10) + " ")) 
   outputLine2 += (ctr Mod 10)& " "
   outputLine3 += chEnum.Current & " "
   ctr += 1
Loop

Console.WriteLine("The length of the string is {0} characters:", _
                  title.Length)
Console.WriteLine(outputLine1)
Console.WriteLine(outputLine2)    
Console.WriteLine(outputLine3)
' The example displays the following output to the console:       
'       The length of the string is 20 characters: 
'                         1                   2 
'       1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 
'       A   T a l e   o f   T w o   C i t i e s

Note, however, that the same operation can be performed somewhat more intuitively by using foreach (in C#) or For Each (in Visual Basic), as the following example shows.

Dim title As String = "A Tale of Two Cities" 
Dim ctr As Integer = 1
Dim outputLine1, outputLine2, outputLine3 As String  

For Each ch As Char In title
   outputLine1 += CStr(iif(ctr < 10 Or ctr Mod 10 <> 0, "  ", CStr(ctr \ 10) + " ")) 
   outputLine2 += (ctr Mod 10)& " "
   outputLine3 += ch & " "
   ctr += 1
Next

Console.WriteLine("The length of the string is {0} characters:", _
                  title.Length)
Console.WriteLine(outputLine1)
Console.WriteLine(outputLine2)    
Console.WriteLine(outputLine3)
' The example displays the following output to the console:       
'       The length of the string is 20 characters: 
'                         1                   2 
'       1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 
'       A   T a l e   o f   T w o   C i t i e s

.NET Framework

Supported in: 4.6, 4.5, 4, 3.5, 3.0, 2.0, 1.1

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

XNA Framework

Supported in: 3.0, 2.0, 1.0
Show:
© 2015 Microsoft