CharEnumerator.Current Vlastnost
Definice
Důležité
Některé informace platí pro předběžně vydaný produkt, který se může zásadně změnit, než ho výrobce nebo autor vydá. Microsoft neposkytuje žádné záruky, výslovné ani předpokládané, týkající se zde uváděných informací.
Získá aktuálně odkazovaný znak v řetězci výčtu tohoto CharEnumerator objektu.
public:
property char Current { char get(); };
public char Current { get; }
member this.Current : char
Public ReadOnly Property Current As Char
Hodnota vlastnosti
Znak Unicode, na který tento CharEnumerator objekt odkazuje.
Implementuje
Výjimky
Index je neplatný; to znamená, že je před prvním nebo za posledním znakem výčtu řetězce.
Příklady
Následující příklad používá CharEnumerator třídu k vytvoření výčtu jednotlivých znaků v řetězci. Vytvoří instanci CharEnumerator objektu voláním String.GetEnumerator metody, přesune se z jednoho znaku na další voláním MoveNext metody a zobrazí aktuální znak načtením hodnoty Current vlastnosti.
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
Všimněte si však, že stejnou operaci lze provést poněkud intuitivně pomocí foreach (v jazyce C#) nebo For Each (v jazyce Visual Basic), jak ukazuje následující příklad.
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
Poznámky
Třída CharEnumerator udržuje interní index výčtového řetězce a Current vlastnost vrátí znak, který je aktuálně odkazován indexem. Tato vlastnost by měla být vyvolána pouze v případě, že je index platný; v opačném případě je vyvolán výjimka.
Index je vždy neplatný pro prázdný řetězec (""). Index je také neplatný po String.GetEnumerator zavolání nebo Reset metody. Po volání některé z těchto metod vyvoláte metodu MoveNext pro úpravu indexu na první znak v výčtu řetězce. Index je platný vždy, když MoveNext metoda vrátí true.
Current nepřesune index a po sobě jdoucí volání, která Current vrátí stejný znak, dokud MoveNext, Resetnebo String.GetEnumerator je volána.