CharEnumerator 클래스

정의

String 개체의 반복 및 개별 문자 읽기를 지원합니다. 이 클래스는 상속될 수 없습니다.

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
상속
CharEnumerator
특성
구현

예제

다음 예제에서는 클래스를 CharEnumerator 사용하여 문자열의 개별 문자를 열거합니다. 메서드를 호출 String.GetEnumerator 하여 개체를 CharEnumerator 인스턴스화하고, 메서드를 호출 MoveNext 하여 한 문자에서 다음 문자로 이동하고, 속성 값을 Current 검색하여 현재 문자를 표시합니다.

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

그러나 다음 예제와 같이 (C#에서) 또는 For Each (Visual Basic) 사용하여 foreach 동일한 작업을 좀 더 직관적으로 수행할 수 있습니다.

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 CharEnumerator 는 참조된 String 개체의 문자에 대한 읽기 전용 액세스를 제공합니다. 예를 들어 foreach 컬렉션 CharEnumerator 의 요소를 반복하는 Microsoft Visual Basic 및 C# 프로그래밍 언어의 문은 해당 개체의 문자를 반복하기 위해 개체에서 String 검색합니다.

중요

클래스는 CharEnumerator 개별 16비 Char 트 인스턴스를 열거합니다. 그래프(즉, 하나 이상의 빗질 문자가 뒤에 오를 문자) 또는 서로게이트 쌍(즉, 유니코드 기본 다국어 평면 외부의 문자)을 단일 문자로 간주하지 않습니다. 이러한 형식의 문자를 단일 단위로 처리하는 열거자의 경우 클래스를 StringInfo 사용합니다.

에 대한 CharEnumerator공용 생성자가 없습니다. 대신 개체의 GetEnumerator 메서드를 String 호출하여 문자열을 CharEnumerator 참조하도록 초기화된 메서드를 가져옵니다.

A CharEnumerator 는 참조하는 문자열 CharEnumerator 의 문자에 대한 내부 인덱스를 유지 관리합니다. 인덱스의 상태는 문자열의 첫 번째 문자 앞이나 마지막 문자 뒤의 문자 위치를 논리적으로 참조할 때 유효하지 않으며 문자열 내의 문자를 참조할 때 유효합니다. 인덱스는 첫 번째 문자 앞에 논리적으로 위치로 초기화되고 반복이 완료되면 마지막 문자 뒤의 위치로 설정됩니다. 인덱스가 유효하지 않은 동안 문자에 액세스하려고 하면 예외가 throw됩니다.

이 메서드는 MoveNext 인덱스를 하나씩 증가시켜 첫 번째 문자와 후속 문자에 차례로 액세스합니다. 이 메서드는 Reset 인덱스가 첫 번째 문자 앞에 논리적으로 위치로 설정합니다. 이 속성은 Current 현재 인덱스로 참조되는 문자를 검색합니다. 메서드는 Clone .의 복사본을 CharEnumerator만듭니다.

참고

하나 이상의 스레드에서 CharEnumerator 여러 독립 인스턴스가 단일 인스턴스 String에 액세스할 수 있습니다. 이 클래스는 인터페이스를 지원하도록 구현됩니다 IEnumerator . 열거자 사용에 대한 자세한 내용은 항목을 참조 IEnumerator 하세요.

속성

Current

CharEnumerator 개체에서 열거한 문자열의 현재 참조되는 문자를 가져옵니다.

메서드

Clone()

현재 CharEnumerator 개체의 복사본을 만듭니다.

Dispose()

CharEnumerator 클래스의 현재 인스턴스에서 사용하는 모든 리소스를 해제합니다.

Equals(Object)

지정된 개체가 현재 개체와 같은지 확인합니다.

(다음에서 상속됨 Object)
GetHashCode()

기본 해시 함수로 작동합니다.

(다음에서 상속됨 Object)
GetType()

현재 인스턴스의 Type을 가져옵니다.

(다음에서 상속됨 Object)
MemberwiseClone()

현재 Object의 단순 복사본을 만듭니다.

(다음에서 상속됨 Object)
MoveNext()

현재 CharEnumerator 개체의 내부 인덱스를 열거된 문자열의 다음 문자로 증가시킵니다.

Reset()

인덱스를 논리적으로 열거형 문자열의 첫째 문자 앞으로 초기화합니다.

ToString()

현재 개체를 나타내는 문자열을 반환합니다.

(다음에서 상속됨 Object)

명시적 인터페이스 구현

IDisposable.Dispose()

CharEnumerator 클래스에서 사용하는 모든 리소스를 해제합니다.

IEnumerator.Current

CharEnumerator 개체에서 열거한 문자열의 현재 참조되는 문자를 가져옵니다. 이 멤버에 대한 설명은 Current를 참조하세요.

적용 대상

추가 정보