String.GetEnumerator 메서드
정의
중요
일부 정보는 릴리스되기 전에 상당 부분 수정될 수 있는 시험판 제품과 관련이 있습니다. Microsoft는 여기에 제공된 정보에 대해 어떠한 명시적이거나 묵시적인 보증도 하지 않습니다.
이 문자열의 개별 문자에서 반복될 수 있는 개체를 검색합니다.
public:
CharEnumerator ^ GetEnumerator();
public CharEnumerator GetEnumerator ();
member this.GetEnumerator : unit -> CharEnumerator
Public Function GetEnumerator () As CharEnumerator
반환
열거자 개체입니다.
예제
다음 예제에서는 여러 문자열의 문자를 반복하고 개별 문자에 대한 정보를 표시합니다. 메서드를 호출하는 대신 언어 반복 구문을 GetEnumerator 사용합니다.
using namespace System;
void EnumerateAndDisplay(String^ phrase)
{
Console::WriteLine("The characters in the string \"{0}\" are:",
phrase);
int CharCount = 0;
int controlChars = 0;
int alphanumeric = 0;
int punctuation = 0;
for each (Char ch in phrase) {
Console::Write("'{0}' ", (! Char::IsControl(ch)) ? ch.ToString() :
"0x" + Convert::ToUInt16(ch).ToString("X4"));
if (Char::IsLetterOrDigit(ch))
alphanumeric++;
else if (Char::IsControl(ch))
controlChars++;
else if (Char::IsPunctuation(ch))
punctuation++;
CharCount++;
}
Console::WriteLine("\n Total characters: {0,3}", CharCount);
Console::WriteLine(" Alphanumeric characters: {0,3}", alphanumeric);
Console::WriteLine(" Punctuation characters: {0,3}", punctuation);
Console::WriteLine(" Control Characters: {0,3}\n", controlChars);
}
int main()
{
EnumerateAndDisplay("Test Case");
EnumerateAndDisplay("This is a sentence.");
EnumerateAndDisplay("Has\ttwo\ttabs");
EnumerateAndDisplay("Two\nnew\nlines");
}
// The example displays the following output:
// The characters in the string "Test Case" are:
// 'T' 'e' 's' 't' ' ' 'C' 'a' 's' 'e'
// Total characters: 9
// Alphanumeric characters: 8
// Punctuation characters: 0
// Control Characters: 0
//
// The characters in the string "This is a sentence." are:
// 'T' 'h' 'i' 's' ' ' 'i' 's' ' ' 'a' ' ' 's' 'e' 'n' 't' 'e' 'n' 'c' 'e' '.'
// Total characters: 19
// Alphanumeric characters: 15
// Punctuation characters: 1
// Control Characters: 0
//
// The characters in the string "Has two tabs" are:
// 'H' 'a' 's' '0x0009' 't' 'w' 'o' '0x0009' 't' 'a' 'b' 's'
// Total characters: 12
// Alphanumeric characters: 10
// Punctuation characters: 0
// Control Characters: 2
//
// The characters in the string "Two
// new
// lines" are:
// 'T' 'w' 'o' '0x000A' 'n' 'e' 'w' '0x000A' 'l' 'i' 'n' 'e' 's'
// Total characters: 13
// Alphanumeric characters: 11
// Punctuation characters: 0
// Control Characters: 2
using System;
class Example
{
public static void Main()
{
EnumerateAndDisplay("Test Case");
EnumerateAndDisplay("This is a sentence.");
EnumerateAndDisplay("Has\ttwo\ttabs" );
EnumerateAndDisplay("Two\nnew\nlines");
}
static void EnumerateAndDisplay(String phrase)
{
Console.WriteLine("The characters in the string \"{0}\" are:",
phrase);
int CharCount = 0;
int controlChars = 0;
int alphanumeric = 0;
int punctuation = 0;
foreach (var ch in phrase) {
Console.Write(Char.IsControl(ch) ? $"{ch}" : $"0x{(ushort)ch:X4}");
if (Char.IsLetterOrDigit(ch))
alphanumeric++;
else if (Char.IsControl(ch))
controlChars++;
else if (Char.IsPunctuation(ch))
punctuation++;
CharCount++;
}
Console.WriteLine("\n Total characters: {0,3}", CharCount);
Console.WriteLine(" Alphanumeric characters: {0,3}", alphanumeric);
Console.WriteLine(" Punctuation characters: {0,3}", punctuation);
Console.WriteLine(" Control Characters: {0,3}\n", controlChars);
}
}
// The example displays the following output:
// The characters in the string "Test Case" are:
// 'T' 'e' 's' 't' ' ' 'C' 'a' 's' 'e'
// Total characters: 9
// Alphanumeric characters: 8
// Punctuation characters: 0
// Control Characters: 0
//
// The characters in the string "This is a sentence." are:
// 'T' 'h' 'i' 's' ' ' 'i' 's' ' ' 'a' ' ' 's' 'e' 'n' 't' 'e' 'n' 'c' 'e' '.'
// Total characters: 19
// Alphanumeric characters: 15
// Punctuation characters: 1
// Control Characters: 0
//
// The characters in the string "Has two tabs" are:
// 'H' 'a' 's' '0x0009' 't' 'w' 'o' '0x0009' 't' 'a' 'b' 's'
// Total characters: 12
// Alphanumeric characters: 10
// Punctuation characters: 0
// Control Characters: 2
//
// The characters in the string "Two
// new
// lines" are:
// 'T' 'w' 'o' '0x000A' 'n' 'e' 'w' '0x000A' 'l' 'i' 'n' 'e' 's'
// Total characters: 13
// Alphanumeric characters: 11
// Punctuation characters: 0
// Control Characters: 2
Module Example
Sub Main()
EnumerateAndDisplay("Test Case")
EnumerateAndDisplay("This is a sentence.")
EnumerateAndDisplay("Has" & vbTab & "two" & vbTab & "tabs")
EnumerateAndDisplay("Two" & vbLf & "new" & vbLf & "lines")
End Sub
Sub EnumerateAndDisplay(phrase As String)
Console.WriteLine("The characters in the string ""{0}"" are:", phrase)
Dim charCount As Integer = 0
Dim controlChars As Integer = 0
Dim alphanumeric As Integer = 0
Dim punctuation As Integer = 0
For Each ch In phrase
Console.Write("'{0}' ", If(Not Char.IsControl(ch), ch,
"0x" + Convert.ToUInt16(ch).ToString("X4")))
If Char.IsLetterOrDigit(ch) Then
alphanumeric += 1
Else If Char.IsControl(ch) Then
controlChars += 1
Else If Char.IsPunctuation(ch) Then
punctuation += 1
End If
CharCount += 1
Next
Console.WriteLine()
Console.WriteLine(" Total characters: {0,3}", CharCount)
Console.WriteLine(" Alphanumeric characters: {0,3}", alphanumeric)
Console.WriteLine(" Punctuation characters: {0,3}", punctuation)
Console.WriteLine(" Control Characters: {0,3}", controlChars)
Console.WriteLine()
End Sub
End Module
' This example displays the following output:
' The characters in the string "Test Case" are:
' 'T' 'e' 's' 't' ' ' 'C' 'a' 's' 'e'
' Total characters: 9
' Alphanumeric characters: 8
' Punctuation characters: 0
' Control Characters: 0
'
' The characters in the string "This is a sentence." are:
' 'T' 'h' 'i' 's' ' ' 'i' 's' ' ' 'a' ' ' 's' 'e' 'n' 't' 'e' 'n' 'c' 'e' '.'
' Total characters: 19
' Alphanumeric characters: 15
' Punctuation characters: 1
' Control Characters: 0
'
' The characters in the string "Has two tabs" are:
' 'H' 'a' 's' '0x0009' 't' 'w' 'o' '0x0009' 't' 'a' 'b' 's'
' Total characters: 12
' Alphanumeric characters: 10
' Punctuation characters: 0
' Control Characters: 2
'
' The characters in the string "Two
' new
' lines" are:
' 'T' 'w' 'o' '0x000A' 'n' 'e' 'w' '0x000A' 'l' 'i' 'n' 'e' 's'
' Total characters: 13
' Alphanumeric characters: 11
' Punctuation characters: 0
' Control Characters: 2
설명
팁
GetEnumerator메서드를 호출하여 CharEnumerator 문자열을 열거하는 데 사용하는 개체를 검색하는 대신 언어의 반복 구문(C#, C++/CLR 및 Visual Basic)을 사용해야 합니다. c#의 foreach, C++/CLR의 각 for 및 for Each in Visual Basic).
이 메서드를 사용하면 문자열의 개별 문자를 반복할 수 있습니다. 예를 들어 Visual Basic 및 For Each
C# foreach
문은 이 메서드를 호출하여 CharEnumerator 이 문자열 인스턴스의 문자에 대한 읽기 전용 액세스를 제공할 수 있는 개체를 반환합니다.