Bagikan melalui


CharEnumerator Kelas

Definisi

Mendukung iterasi atas String objek dan membaca karakter individualnya. Kelas ini tidak dapat diwariskan.

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
Warisan
CharEnumerator
Atribut
Penerapan

Contoh

Contoh berikut menggunakan CharEnumerator kelas untuk menghitung karakter individual dalam string. Ini membuat instans CharEnumerator objek dengan memanggil String.GetEnumerator metode , berpindah dari satu karakter ke karakter berikutnya dengan memanggil MoveNext metode , dan menampilkan karakter saat ini dengan mengambil nilai Current properti .

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

Perhatikan, bagaimanapun, bahwa operasi yang sama dapat dilakukan agak lebih intuitif dengan menggunakan foreach (dalam C#) atau For Each (di Visual Basic), seperti yang ditunjukkan contoh berikut.

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

Keterangan

menyediakan CharEnumerator akses baca-saja ke karakter dalam objek yang dirujuk String . Misalnya, foreach pernyataan bahasa pemrograman Microsoft Visual Basic dan C#, yang berulang melalui elemen koleksi, mengambil CharEnumerator dari String objek untuk melakukan iterasi melalui karakter dalam objek tersebut.

Penting

Kelas menghitung CharEnumerator instans 16-bit Char individual. Ini tidak mempertimbangkan graphemes (yaitu, karakter diikuti oleh satu atau beberapa karakter yang melarang) atau pasangan pengganti (yaitu, karakter di luar Bidang Multibahasa Dasar Unicode) sebagai karakter tunggal. Untuk enumerator yang menangani jenis karakter ini sebagai satu unit, gunakan StringInfo kelas .

Tidak ada konstruktor publik untuk CharEnumerator. Sebagai gantinyaGetEnumerator, panggil String metode objek untuk mendapatkan CharEnumerator yang diinisialisasi untuk mereferensikan string.

mempertahankan CharEnumerator indeks internal ke karakter dalam string CharEnumerator referensi. Status indeks tidak valid ketika mereferensikan posisi karakter secara logis sebelum karakter pertama atau setelah karakter terakhir dalam string, dan valid saat mereferensikan karakter dalam string. Indeks diinisialisasi ke posisi secara logis sebelum karakter pertama, dan diatur ke posisi setelah karakter terakhir ketika perulangan selesai. Pengecualian dilemparkan jika Anda mencoba mengakses karakter saat indeks tidak valid.

Metode ini MoveNext menaikkan indeks satu per satu, sehingga karakter pertama dan berikutnya diakses secara bergantian. Metode Reset mengatur indeks ke posisi secara logis sebelum karakter pertama. Properti Current mengambil karakter yang saat ini dirujuk oleh indeks. Metode ini Clone membuat salinan CharEnumerator.

Nota

Beberapa instans independen dari CharEnumerator satu atau beberapa utas dapat memiliki akses ke satu instans String. Kelas ini diimplementasikan untuk mendukung IEnumerator antarmuka. Untuk informasi selengkapnya mengenai penggunaan enumerator, lihat topiknya IEnumerator .

Properti

Nama Deskripsi
Current

Mendapatkan karakter yang saat ini dirujuk dalam string yang dijumlahkan oleh objek ini CharEnumerator .

Metode

Nama Deskripsi
Clone()

Membuat salinan objek saat ini CharEnumerator .

Dispose()

Merilis semua sumber daya yang digunakan oleh instans CharEnumerator kelas saat ini.

Equals(Object)

Menentukan apakah objek yang ditentukan sama dengan objek saat ini.

(Diperoleh dari Object)
GetHashCode()

Berfungsi sebagai fungsi hash default.

(Diperoleh dari Object)
GetType()

Mendapatkan Type instans saat ini.

(Diperoleh dari Object)
MemberwiseClone()

Membuat salinan dangkal dari Objectsaat ini.

(Diperoleh dari Object)
MoveNext()

Meningkatkan indeks internal objek saat ini CharEnumerator ke karakter berikutnya dari string enumerasi.

Reset()

Menginisialisasi indeks ke posisi secara logis sebelum karakter pertama dari string enumerasi.

ToString()

Mengembalikan string yang mewakili objek saat ini.

(Diperoleh dari Object)

Implementasi Antarmuka Eksplisit

Nama Deskripsi
IDisposable.Dispose()

Merilis semua sumber daya yang CharEnumerator digunakan oleh kelas .

IEnumerator.Current

Mendapatkan karakter yang saat ini dirujuk dalam string yang dijumlahkan oleh objek ini CharEnumerator . Untuk deskripsi anggota ini, lihat Current.

Berlaku untuk

Lihat juga