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)

public bool MoveNext()

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.

string title = "A Tale of Two Cities";
CharEnumerator chEnum = title.GetEnumerator();
int ctr = 1;
string outputLine1 = null;
string outputLine2 = null;
string outputLine3 = null; 

while (chEnum.MoveNext())
{
   outputLine1 += ctr < 10 || ctr % 10 != 0 ? "  " : (ctr / 10) + " ";
   outputLine2 += (ctr % 10) + " ";
   outputLine3 += chEnum.Current + " ";
   ctr++;
}

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.

string title = "A Tale of Two Cities";
int ctr = 1;
string outputLine1 = null;
string outputLine2 = null;
string outputLine3 = null; 

foreach (char ch in title)
{
   outputLine1 += ctr < 10 || ctr % 10 != 0 ? "  " : (ctr / 10) + " ";
   outputLine2 += (ctr % 10) + " ";
   outputLine3 += ch + " ";
   ctr++;
}

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

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98, Windows CE, Windows Mobile for Smartphone, Windows Mobile for Pocket PC, Xbox 360, Zune

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

.NET Framework

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

.NET Compact Framework

Supported in: 3.5, 2.0, 1.0

XNA Framework

Supported in: 3.0, 2.0, 1.0
Was this page helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft