CharEnumerator.MoveNext Methode
Definition
Wichtig
Einige Informationen beziehen sich auf Vorabversionen, die vor dem Release ggf. grundlegend überarbeitet werden. Microsoft übernimmt hinsichtlich der hier bereitgestellten Informationen keine Gewährleistungen, seien sie ausdrücklich oder konkludent.
Erhöht den internen Index des aktuellen CharEnumerator Objekts auf das nächste Zeichen der aufgezählten Zeichenfolge.
public:
virtual bool MoveNext();
public bool MoveNext();
abstract member MoveNext : unit -> bool
override this.MoveNext : unit -> bool
Public Function MoveNext () As Boolean
Gibt zurück
truewenn der Index erfolgreich inkrementiert und innerhalb der aufgezählten Zeichenfolge erfolgt; andernfalls . false
Implementiert
Beispiele
Im folgenden Beispiel wird die CharEnumerator Klasse zum Aufzählen der einzelnen Zeichen in einer Zeichenfolge verwendet. Es instanziiert ein CharEnumerator Objekt durch Aufrufen der String.GetEnumerator Methode, wechselt von einem Zeichen zum nächsten durch Aufrufen der MoveNext Methode und zeigt das aktuelle Zeichen an, indem der Wert der Current Eigenschaft abgerufen wird.
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
let title = "A Tale of Two Cities"
let chEnum = title.GetEnumerator()
printfn $"The length of the string is {title.Length} characters:"
let mutable outputLine1 = ""
let mutable outputLine2 = ""
let mutable outputLine3 = ""
let mutable i = 1
while chEnum.MoveNext() do
outputLine1 <- outputLine1 + if i < 10 || i % 10 <> 0 then " " else $"{i / 10} "
outputLine2 <- outputLine2 + $"{i % 10} ";
outputLine3 <- outputLine3 + $"{chEnum.Current} "
i <- i + 1
printfn "%s" outputLine1
printfn "%s" outputLine2
printfn "%s" 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 etwas intuitiver ausgeführt werden kann, indem foreach (in C#) oder For Each (in Visual Basic) verwendet wird, wie im folgenden Beispiel gezeigt.
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
let title = "A Tale of Two Cities"
let chEnum = title.GetEnumerator()
printfn $"The length of the string is {title.Length} characters:"
let mutable outputLine1 = ""
let mutable outputLine2 = ""
let mutable outputLine3 = ""
let mutable i = 1
for ch in title do
outputLine1 <- outputLine1 + if i < 10 || i % 10 <> 0 then " " else $"{i / 10} "
outputLine2 <- outputLine2 + $"{i % 10} ";
outputLine3 <- outputLine3 + $"{ch} "
i <- i + 1
printfn "%s" outputLine1
printfn "%s" outputLine2
printfn "%s" 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 für die aufgezählte Zeichenfolge, und die MoveNext Methode erhöht den Index um eins. Aufrufen nach dem Aufrufen MoveNextGetEnumerator oder Reset Erhöhen der aktuellen Zeichenposition auf das erste Zeichen in der aufgezählten Zeichenfolge. Überprüfen Sie, ob der Rückgabewert gültig ist true , um zu bestimmen, ob die aktuelle Zeichenposition gültig ist.
Wenn der Index bereits über das letzte Zeichen der aufgezählten Zeichenfolge hinausgeht, wird der Index nicht geändert und false zurückgegeben.
Beachten Sie, dass der Status der CharEnumerator enumerierten Zeichenfolge immer ungültig ist, wenn die aufgezählte Zeichenfolge leer ("") ist. Dies liegt daran, dass der interne Index für das CharEnumerator erste Zeichen der aufgezählten Zeichenfolge anfangs vor dem ersten Zeichen liegt und daher ungültig ist. MoveNext Legt den Index logisch nach dem letzten (nicht vorhandenen) Zeichen der aufgezählten Zeichenfolge fest, die ebenfalls ungültig ist.