CharEnumerator Sınıf

Tanım

Bir String nesne üzerinde yinelemeyi ve tek tek karakterlerini okumayı destekler. Bu sınıf devralınamaz.

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
Devralma
CharEnumerator
Öznitelikler
Uygulamalar

Örnekler

Aşağıdaki örnek, bir dizedeki CharEnumerator tek tek karakterleri listelemek için sınıfını kullanır. yöntemini çağırarak bir nesne örneği oluşturur, yöntemini çağırarak MoveNextString.GetEnumerator bir CharEnumerator karakterden diğerine geçer ve özelliğin Current değerini alarak geçerli karakteri görüntüler.

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

Ancak, aşağıdaki örnekte gösterildiği gibi aynı işlemin (C#'ta) veya For Each (Visual Basic'te) kullanılarak foreach biraz daha sezgisel bir şekilde gerçekleştirilebileceğini unutmayın.

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

Açıklamalar

, CharEnumerator başvuruda bulunılan String nesnedeki karakterlere salt okunur erişim sağlar. Örneğin, foreach bir koleksiyonun öğelerinde yinelenen Microsoft Visual Basic ve C# programlama dillerinin deyimi, nesnedeki karakterleri yinelemek için nesneden bir CharEnumeratorString alır.

Önemli

sınıfı tek CharEnumerator tek 16 bit Char örnekleri numaralandırır. Grafemeleri (bir veya daha fazla tarama karakteri tarafından takip edilen bir karakter) veya vekil çiftleri (Unicode Temel Çok Dilli Düzlem dışındaki karakterler) tek karakter olarak dikkate almaz. Bu tür karakterleri tek bir birim olarak işleyen bir numaralandırıcı için sınıfını StringInfo kullanın.

için CharEnumeratorgenel oluşturucu yok. Bunun yerine, dizeye başvurmak üzere başlatılan bir CharEnumerator almak için nesnesinin GetEnumerator yöntemini çağırınString.

A CharEnumerator , başvuruda bulunan dizedeki karakterlerin iç dizinini CharEnumerator tutar. Dizinin durumu, dizedeki ilk karakterden önce veya son karakterden sonra mantıksal olarak bir karakter konumuna başvurduğunda geçersizdir ve dize içindeki bir karaktere başvurduğunda geçerlidir. Dizin ilk karakterden önce mantıksal olarak bir konuma başlatılır ve yineleme tamamlandığında son karakterden sonra bir konuma ayarlanır. Dizin geçersizken bir karaktere erişmeye çalıştığınızda bir özel durum oluşur.

MoveNext yöntemi dizini bir artırır, böylece ilk ve sonraki karakterlere sırayla erişilir. yöntemi, Reset dizini ilk karakterden önce mantıksal olarak bir konuma ayarlar. özelliği, Current dizin tarafından şu anda başvuruda bulunan karakteri alır. yöntemi, Clone öğesinin CharEnumeratorbir kopyasını oluşturur.

Not

Bir veya daha fazla iş parçacığının birkaç bağımsız örneği CharEnumerator , tek bir örneğine Stringerişebilir. Bu sınıf arabirimini desteklemek IEnumerator için uygulanır. Numaralandırıcı kullanımı hakkında daha fazla bilgi için konuya bakın IEnumerator .

Özellikler

Current

Bu CharEnumerator nesne tarafından numaralandırılan dizede şu anda başvurulan karakteri alır.

Yöntemler

Clone()

Geçerli CharEnumerator nesnenin bir kopyasını oluşturur.

Dispose()

CharEnumerator sınıfının geçerli örneği tarafından kullanılan tüm kaynakları serbest bırakır.

Equals(Object)

Belirtilen nesnenin geçerli nesneye eşit olup olmadığını belirler.

(Devralındığı yer: Object)
GetHashCode()

Varsayılan karma işlevi işlevi görür.

(Devralındığı yer: Object)
GetType()

Type Geçerli örneğini alır.

(Devralındığı yer: Object)
MemberwiseClone()

Geçerli Objectöğesinin sığ bir kopyasını oluşturur.

(Devralındığı yer: Object)
MoveNext()

Geçerli CharEnumerator nesnenin iç dizinini numaralandırılmış dizenin sonraki karakterine artırır.

Reset()

Dizini numaralandırılmış dizenin ilk karakterinden önce mantıksal olarak bir konuma başlatır.

ToString()

Geçerli nesneyi temsil eden dizeyi döndürür.

(Devralındığı yer: Object)

Belirtik Arabirim Kullanımları

IDisposable.Dispose()

sınıfı tarafından CharEnumerator kullanılan tüm kaynakları serbest bırakır.

IEnumerator.Current

Bu CharEnumerator nesne tarafından numaralandırılan dizede şu anda başvurulan karakteri alır. Bu üyenin açıklaması için bkz Current. .

Şunlara uygulanır

Ayrıca bkz.