CharEnumerator.Current Eigenschaft

Definition

Ruft das Zeichen in der von diesem CharEnumerator-Objekt durchlaufenen Zeichenfolge ab, auf das zurzeit verwiesen wird.Gets the currently referenced character in the string enumerated by this CharEnumerator object.

public:
 property char Current { char get(); };
public char Current { get; }
member this.Current : char
Public ReadOnly Property Current As Char

Eigenschaftswert

Das Unicode-Zeichen, auf das zurzeit durch dieses CharEnumerator-Objekt verwiesen wird.The Unicode character currently referenced by this CharEnumerator object.

Implementiert

Ausnahmen

Der Index ist ungültig; d. h. er befindet sich vor den ersten oder hinter dem letzten Zeichen der aufgelisteten Zeichenfolge.The index is invalid; that is, it is before the first or after the last character of the enumerated string.

Beispiele

Im folgenden Beispiel wird die CharEnumerator -Klasse verwendet, um die einzelnen Zeichen in einer Zeichenfolge aufzulisten.The following example uses the CharEnumerator class to enumerate the individual characters in a string. Sie instanziiert ein CharEnumerator -Objekt durch Aufrufen String.GetEnumerator der-Methode, wechselt von einem Zeichen zum nächsten, indem MoveNext die-Methode aufgerufen wird, und zeigt das aktuelle Current Zeichen an, indem der Wert der-Eigenschaft abgerufen wird.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
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
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

Beachten Sie jedoch, dass derselbe Vorgang mit foreach (in C#) oder For Each (in Visual Basic) etwas intuitiver ausgeführt werden kann, wie im folgenden Beispiel gezeigt.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
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
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

Hinweise

Die CharEnumerator -Klasse verwaltet einen internen Index zur enumerationszeichenfolge Current , und die-Eigenschaft gibt das Zeichen zurück, auf das zurzeit vom Index verwiesen wird.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. Diese Eigenschaft sollte nur aufgerufen werden, wenn der Index gültig ist. Andernfalls wird eine Ausnahme ausgelöst.This property should be invoked only when the index is valid; otherwise, an exception is thrown.

Der Index ist für eine leere Zeichenfolge ("") immer ungültig.The index is always invalid for an empty string (""). Der Index ist auch ungültig, nachdem String.GetEnumerator die Reset -Methode oder die-Methode aufgerufen wurde.The index is also invalid after the String.GetEnumerator or Reset method is called. Nachdem Sie eine dieser Methoden aufgerufen haben, rufen Sie MoveNext die-Methode auf, um den Index auf das erste Zeichen in der aufgelisteten Zeichenfolge anzupassen.After either of these methods is called, invoke the MoveNext method to adjust the index to the first character in the enumerated string. Der Index ist gültig, wenn MoveNext die- trueMethode zurückgibt.The index is valid whenever the MoveNext method returns true.

CurrentVerschiebt den Index nicht, und aufeinanderfolgende Aufrufe von Current geben das gleiche Zeichen zurück, bis Reset MoveNext, oder String.GetEnumerator aufgerufen wird.Current does not move the index, and consecutive calls to Current return the same character until MoveNext, Reset, or String.GetEnumerator is called.

Gilt für: