CharEnumerator Class
TOC
Collapse the table of content
Expand the table of content

CharEnumerator Class

 

Supports iterating over a String object and reading its individual characters. This class cannot be inherited.

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

System::Object
  System::CharEnumerator

[SerializableAttribute]
[ComVisibleAttribute(true)]
public ref class CharEnumerator sealed : IEnumerator, ICloneable, 
	IEnumerator<wchar_t>, IDisposable

NameDescription
System_CAPS_pubpropertyCurrent

Gets the currently referenced character in the string enumerated by this CharEnumerator object.

NameDescription
System_CAPS_pubmethodClone()

Creates a copy of the current CharEnumerator object.

System_CAPS_pubmethodDispose()

Releases all resources used by the current instance of the CharEnumerator class.

System_CAPS_pubmethodEquals(Object^)

Determines whether the specified object is equal to the current object.(Inherited from Object.)

System_CAPS_pubmethodGetHashCode()

Serves as the default hash function. (Inherited from Object.)

System_CAPS_pubmethodGetType()

Gets the Type of the current instance.(Inherited from Object.)

System_CAPS_pubmethodMoveNext()

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

System_CAPS_pubmethodReset()

Initializes the index to a position logically before the first character of the enumerated string.

System_CAPS_pubmethodToString()

Returns a string that represents the current object.(Inherited from Object.)

NameDescription
System_CAPS_pubinterfaceSystem_CAPS_privpropertyIEnumerator::Current

This API supports the product infrastructure and is not intended to be used directly from your code. Gets the currently referenced character in the string enumerated by this CharEnumerator object. For a description of this member, see IEnumerator::Current.

A CharEnumerator provides read-only access to the characters in a referenced String object. For example, the foreach statement of the Microsoft Visual Basic and C# programming languages, which iterates through the elements of a collection, retrieves a CharEnumerator from a String object in order to iterate through the characters in that object.

There is no public constructor for CharEnumerator. Instead, call a String object's GetEnumerator method to obtain a CharEnumerator that is initialized to reference the string.

A CharEnumerator maintains an internal index to the characters in the string the CharEnumerator references. The state of the index is invalid when it references a character position logically before the first character or after the last character in the string, and valid when it references a character within the string. The index is initialized to a position logically before the first character, and is set to a position after the last character when the iteration is complete. An exception is thrown if you attempt to access a character while the index is invalid.

The MoveNext method increments the index by one, so the first and subsequent characters are accessed in turn. The Reset method sets the index to a position logically before the first character. The Current property retrieves the character currently referenced by index. The Clone method creates a copy of the CharEnumerator.

System_CAPS_noteNote

Several independent instances of CharEnumerator across one or more threads can have access to a single instance of String. This class is implemented to support the IEnumerator interface. For more information regarding the use of an enumerator, see the IEnumerator topic.

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 = nullptr;
String ^ outputLine2 = nullptr;
String ^ outputLine3 = nullptr; 

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 = nullptr;
String ^ outputLine2 = nullptr;
String ^ outputLine3 = nullptr; 

for each (wchar_t 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

.NET Framework
Available since 1.1

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Return to top
Show:
© 2016 Microsoft