Sdílet prostřednictvím


CharEnumerator Třída

Definice

Podporuje iterování objektu String a čtení jednotlivých znaků. Tuto třídu nelze zdě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 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

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

Důležité

Třída CharEnumerator vyčísluje jednotlivé 16bitové Char instance. Nepovažuje grafy (tj. znak následovaný jedním nebo více znaky) nebo náhradní dvojice (tj. znaky mimo základní vícejazyčnou rovinu Unicode) jako jednotlivé 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 StringGetEnumerator k získání CharEnumerator inicializovaného 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 je platný, pokud odkazuje na znak v řetězci. Index je inicializován na pozici logicky před prvním znakem a je nastaven na pozici po posledním znaku po dokončení iterace. Pokud se pokusíte o 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 zase přístupné. Metoda Reset nastaví index na pozici logicky před prvním znakem. Vlastnost Current načte znak, na který odkazuje index. Metoda Clone vytvoří kopii CharEnumerator.

Poznámka:

Několik nezávislých instancí napříč jedním nebo více vlákny CharEnumerator 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í enumerátoru najdete v IEnumerator tématu.

Vlastnosti

Name Description
Current

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

Metody

Name Description
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čuje, zda je zadaný objekt roven aktuálnímu objektu.

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

Slouží jako výchozí funkce hash.

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

Získá Type aktuální instance.

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

Vytvoří mělkou kopii aktuálního 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í

Name Description
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 naleznete v tématu Current.

Platí pro

Viz také