CharEnumerator Klasa

Definicja

Obsługuje iterowanie String obiektu i odczytywanie jego poszczególnych znaków. Klasa ta nie może być dziedziczona.

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
Dziedziczenie
CharEnumerator
Atrybuty
Implementuje

Przykłady

W poniższym przykładzie użyto CharEnumerator klasy , aby wyliczyć poszczególne znaki w ciągu. Tworzy wystąpienie CharEnumerator obiektu przez wywołanie String.GetEnumerator metody , przenosi się z jednego znaku do następnego przez wywołanie MoveNext metody i wyświetla bieżący znak przez pobranie wartości Current właściwości.

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

Należy jednak pamiętać, że tę samą operację można wykonać nieco bardziej intuicyjnie przy użyciu metody foreach (w języku C#) lub For Each (w Visual Basic), jak pokazano w poniższym przykładzie.

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

Uwagi

Obiekt zapewnia CharEnumerator dostęp tylko do odczytu do znaków w obiekcie, do których odwołuje się String odwołanie. Na przykład foreach instrukcja języków programowania Microsoft Visual Basic i C#, która iteruje po elementach kolekcji, pobiera obiekt CharEnumerator z String obiektu w celu iterowania znaków w tym obiekcie.

Ważne

Klasa CharEnumerator wylicza pojedyncze wystąpienia 16-bitowe Char . Nie bierze pod uwagę grafów (czyli znaków, po których następuje co najmniej jeden znak combiding) lub pary zastępcze (czyli znaki poza podstawową płaszczyzną wielojęzyczną Unicode) jako pojedyncze znaki. W przypadku modułu wyliczającego, który obsługuje te typy znaków jako pojedynczą jednostkę, użyj StringInfo klasy .

Nie ma publicznego konstruktora dla klasy CharEnumerator. Zamiast tego wywołaj metodę String obiektu GetEnumerator , aby uzyskać zainicjowaną CharEnumerator metodę w celu odwołania się do ciągu.

Obiekt CharEnumerator obsługuje wewnętrzny indeks znaków w ciągu, do których CharEnumerator odwołują się odwołania. Stan indeksu jest nieprawidłowy, gdy odwołuje się do pozycji znaku logicznie przed pierwszym znakiem lub po ostatnim znaku w ciągu i jest prawidłowy, gdy odwołuje się do znaku w ciągu. Indeks jest inicjowany logicznie do pozycji przed pierwszym znakiem i jest ustawiany na pozycję po ostatnim znaku po zakończeniu iteracji. Wyjątek jest zgłaszany, jeśli próbujesz uzyskać dostęp do znaku, gdy indeks jest nieprawidłowy.

Metoda MoveNext zwiększa indeks o jeden, więc dostęp do pierwszych i kolejnych znaków jest uzyskiwany z kolei. Metoda Reset ustawia indeks na pozycję logicznie przed pierwszym znakiem. Właściwość Current pobiera znak, do których obecnie odwołuje się indeks. Metoda Clone tworzy kopię elementu CharEnumerator.

Uwaga

Kilka niezależnych wystąpień jednego lub większej CharEnumerator liczby wątków może mieć dostęp do jednego wystąpienia klasy String. Ta klasa jest implementowana w celu obsługi interfejsu IEnumerator . Aby uzyskać więcej informacji na temat korzystania z modułu wyliczającego, zobacz IEnumerator temat.

Właściwości

Current

Pobiera aktualnie przywoływany znak w ciągu wyliczanym przez ten CharEnumerator obiekt.

Metody

Clone()

Tworzy kopię bieżącego CharEnumerator obiektu.

Dispose()

Zwalnia wszystkie zasoby używane przez bieżące wystąpienie klasy CharEnumerator.

Equals(Object)

Określa, czy dany obiekt jest taki sam, jak bieżący obiekt.

(Odziedziczone po Object)
GetHashCode()

Służy jako domyślna funkcja skrótu.

(Odziedziczone po Object)
GetType()

Type Pobiera wartość bieżącego wystąpienia.

(Odziedziczone po Object)
MemberwiseClone()

Tworzy płytkią kopię bieżącego Objectelementu .

(Odziedziczone po Object)
MoveNext()

Zwiększa wewnętrzny indeks bieżącego CharEnumerator obiektu do następnego znaku wyliczonego ciągu.

Reset()

Inicjuje indeks do pozycji logicznej przed pierwszym znakiem wyliczenia ciągu.

ToString()

Zwraca ciąg reprezentujący bieżący obiekt.

(Odziedziczone po Object)

Jawne implementacje interfejsu

IDisposable.Dispose()

Zwalnia wszystkie zasoby używane przez klasę CharEnumerator .

IEnumerator.Current

Pobiera aktualnie przywoływany znak w ciągu wyliczanym przez ten CharEnumerator obiekt. Aby uzyskać opis tego członka, zobacz Current.

Dotyczy

Zobacz też