CharEnumerator Třída

Definice

Podporuje iterování nad objektem String a čtení jednotlivých znaků. Tuto třídu nelze dědit.

public ref class CharEnumerator sealed : ICloneable, System::Collections::Generic::IEnumerator<char>
public ref class CharEnumerator sealed : ICloneable, System::Collections::IEnumerator
public sealed class CharEnumerator : ICloneable, System.Collections.Generic.IEnumerator<char>
[System.Serializable]
public sealed class CharEnumerator : ICloneable, System.Collections.IEnumerator
[System.Serializable]
[System.Runtime.InteropServices.ComVisible(true)]
public sealed class CharEnumerator : ICloneable, System.Collections.Generic.IEnumerator<char>
type CharEnumerator = class
    interface IEnumerator<char>
    interface IEnumerator
    interface IDisposable
    interface ICloneable
[<System.Serializable>]
type CharEnumerator = class
    interface IEnumerator
    interface ICloneable
[<System.Serializable>]
[<System.Runtime.InteropServices.ComVisible(true)>]
type CharEnumerator = class
    interface ICloneable
    interface IEnumerator<char>
    interface IDisposable
    interface IEnumerator
[<System.Serializable>]
[<System.Runtime.InteropServices.ComVisible(true)>]
type CharEnumerator = class
    interface ICloneable
    interface IEnumerator<char>
    interface IEnumerator
    interface IDisposable
[<System.Serializable>]
[<System.Runtime.InteropServices.ComVisible(true)>]
type CharEnumerator = class
    interface IEnumerator
    interface ICloneable
    interface IEnumerator<char>
    interface IDisposable
Public NotInheritable Class CharEnumerator
Implements ICloneable, IEnumerator(Of Char)
Public NotInheritable Class CharEnumerator
Implements ICloneable, IEnumerator
Dědičnost
CharEnumerator
Atributy
Implementuje

Příklady

Následující příklad používá CharEnumerator třídu k 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ší MoveNext voláním 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 = 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
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ěji pomocí foreach (v jazyce C#) nebo For Each (v Visual Basic), jak ukazuje následující příklad.

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
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

A CharEnumerator poskytuje přístup jen pro čtení k znakům v odkazovaném String objektu. Například foreach příkaz programovacích jazyků Microsoft Visual Basic a C#, který iteruje prvky kolekce, načte CharEnumerator objekt z objektuString, aby iterovat znaky v daném objektu.

Důležité

Třída CharEnumerator vyčísluje jednotlivé 16bitové Char instance. Nebere v úvahu grafeme (tj. znak následovaný jedním nebo více znaky, za kterými následuje jeden nebo více znaků) nebo náhradní páry (tj. znaky mimo základní vícejazyčnou rovinu Unicode) jako jednoduché znaky. Pro enumerátor, který zpracovává tyto typy znaků jako jednu jednotku StringInfo , použijte třídu.

Neexistuje žádný veřejný konstruktor pro CharEnumerator. Místo toho zavolejte metodu objektu String GetEnumerator k získání CharEnumerator inicializované pro odkazování na řetězec.

A CharEnumerator udržuje interní index znaků v řetězci CharEnumerator odkazy. Stav indexu je neplatný, pokud odkazuje na pozici znaku logicky před prvním znakem nebo za posledním znakem v řetězci a platný, když odkazuje na znak v řetězci. Index se inicializuje na pozici logicky před prvním znakem a nastaví se na pozici po posledním znaku po dokončení iterace. Pokud se pokusíte získat přístup ke znaku, když je index neplatný, vyvolá se výjimka.

Metoda MoveNext zvýší index o jeden, takže první a další znaky jsou přístupné zase. Metoda Reset nastaví index na pozici logicky před prvním znakem. Vlastnost načte Current znak, na který odkazuje index. Metoda Clone vytvoří kopii souboru CharEnumerator.

Poznámka

Několik nezávislých instancí CharEnumerator napříč jedním nebo více vlákny může mít přístup k jedné instanci String. Tato třída je implementována pro podporu IEnumerator rozhraní. Další informace o použití výčtu najdete v tématu IEnumerator .

Vlastnosti

Current

Získá aktuálně odkazovaný znak v řetězci výčtu tohoto CharEnumerator objektu.

Metody

Clone()

Vytvoří kopii aktuálního CharEnumerator objektu.

Dispose()

Uvolní všechny prostředky používané aktuální instancí CharEnumerator třídy.

Equals(Object)

Určí, zda se zadaný objekt rovná aktuálnímu objektu.

(Zděděno od Object)
GetHashCode()

Slouží jako výchozí funkce hash.

(Zděděno od Object)
GetType()

Type Získá aktuální instanci.

(Zděděno od Object)
MemberwiseClone()

Vytvoří použádnou kopii aktuálního souboru Object.

(Zděděno od Object)
MoveNext()

Zvýší vnitřní index aktuálního CharEnumerator objektu na další znak výčtu řetězce.

Reset()

Inicializuje index na pozici logicky před prvním znakem výčtu řetězce.

ToString()

Vrátí řetězec, který představuje aktuální objekt.

(Zděděno od Object)

Explicitní implementace rozhraní

IDisposable.Dispose()

Uvolní všechny prostředky používané CharEnumerator třídou.

IEnumerator.Current

Získá aktuálně odkazovaný znak v řetězci výčtu tohoto CharEnumerator objektu. Popis tohoto člena najdete v tématu Current.

Platí pro

Viz také