Export (0) Print
Expand All
This topic has not yet been rated - Rate this topic

CharEnumerator.Current Property

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

Namespace:  System
Assembly:  mscorlib (in mscorlib.dll)
'Declaration
Public ReadOnly Property Current As Char

Property Value

Type: System.Char
The Unicode character currently referenced by this CharEnumerator object.

Implements

IEnumerator(Of T).Current
ExceptionCondition
InvalidOperationException

The index is invalid; that is, it is before the first or after the last character of the enumerated string.

The CharEnumerator class maintains an internal index to the enumerated string, and the Current property returns the character that is currently referenced by the index. This property should be invoked only when the index is valid; otherwise, an exception is thrown.

The index is always invalid for an empty string (""). The index is also invalid after the String.GetEnumerator or Reset method is called. After either of these methods is called, invoke the MoveNext method to adjust the index to the first character in the enumerated string. The index is valid whenever the MoveNext method returns true.

Current does not move the index, and consecutive calls to Current return the same character until MoveNext, Reset, or String.GetEnumerator is called.

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.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 Phone 8.1, Windows Phone 8, 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.

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft. All rights reserved.