StringInfo.GetTextElementEnumerator 메서드

정의

문자열의 텍스트 요소를 반복하는 열거자를 반환합니다.

오버로드

GetTextElementEnumerator(String)

전체 문자열의 텍스트 요소를 반복하는 열거자를 반환합니다.

GetTextElementEnumerator(String, Int32)

지정된 인덱스에서 시작하여 문자열의 텍스트 요소를 반복하는 열거자를 반환합니다.

GetTextElementEnumerator(String)

전체 문자열의 텍스트 요소를 반복하는 열거자를 반환합니다.

public:
 static System::Globalization::TextElementEnumerator ^ GetTextElementEnumerator(System::String ^ str);
public static System.Globalization.TextElementEnumerator GetTextElementEnumerator (string str);
static member GetTextElementEnumerator : string -> System.Globalization.TextElementEnumerator
Public Shared Function GetTextElementEnumerator (str As String) As TextElementEnumerator

매개 변수

str
String

반복할 문자열입니다.

반환

TextElementEnumerator

전체 문자열에 대한 TextElementEnumerator입니다.

예외

str이(가) null인 경우

예제

다음 예제에서는 GetTextElementEnumerator 메서드를 호출하는 방법을 보여 줍니다. 이 예제는에 대해 제공 된 큰 예제의 일부는 StringInfo 클래스입니다.

using namespace System;
using namespace System::Text;
using namespace System::Globalization;


// Show how to enumerate each real character (honoring surrogates)
// in a string.

void EnumTextElements(String^ combiningChars)
{
    // This StringBuilder holds the output results.
    StringBuilder^ sb = gcnew StringBuilder();

    // Use the enumerator returned from GetTextElementEnumerator
    // method to examine each real character.
    TextElementEnumerator^ charEnum =
        StringInfo::GetTextElementEnumerator(combiningChars);
    while (charEnum->MoveNext())
    {
        sb->AppendFormat("Character at index {0} is '{1}'{2}", 
            charEnum->ElementIndex, charEnum->GetTextElement(), 
            Environment::NewLine);
    }

    // Show the results.
    Console::WriteLine("Result of GetTextElementEnumerator:");
    Console::WriteLine(sb);
}


// Show how to discover the index of each real character
// (honoring surrogates) in a string.

void EnumTextElementIndexes(String^ combiningChars)
{
    // This StringBuilder holds the output results.
    StringBuilder^ sb = gcnew StringBuilder();

    // Use the ParseCombiningCharacters method to
    // get the index of each real character in the string.
    array <int>^ textElemIndex =
        StringInfo::ParseCombiningCharacters(combiningChars);

    // Iterate through each real character showing the character
    // and the index where it was found.
    for (int i = 0; i < textElemIndex->Length; i++)
    {
        sb->AppendFormat("Character {0} starts at index {1}{2}",
            i, textElemIndex[i], Environment::NewLine);
    }

    // Show the results.
    Console::WriteLine("Result of ParseCombiningCharacters:");
    Console::WriteLine(sb);
}

int main()
{

    // The string below contains combining characters.
    String^ combiningChars = L"a\u0304\u0308bc\u0327";

    // Show each 'character' in the string.
    EnumTextElements(combiningChars);

    // Show the index in the string where each 'character' starts.
    EnumTextElementIndexes(combiningChars);

};

// This code produces the following output.
//
// Result of GetTextElementEnumerator:
// Character at index 0 is 'a-"'
// Character at index 3 is 'b'
// Character at index 4 is 'c,'
//
// Result of ParseCombiningCharacters:
// Character 0 starts at index 0
// Character 1 starts at index 3
// Character 2 starts at index 4
using System;
using System.Text;
using System.Globalization;

public sealed class App {
   static void Main() {
      // The string below contains combining characters.
      String s = "a\u0304\u0308bc\u0327";

      // Show each 'character' in the string.
      EnumTextElements(s);

      // Show the index in the string where each 'character' starts.
      EnumTextElementIndexes(s);
   }

   // Show how to enumerate each real character (honoring surrogates) in a string.
   static void EnumTextElements(String s) {
      // This StringBuilder holds the output results.
      StringBuilder sb = new StringBuilder();

      // Use the enumerator returned from GetTextElementEnumerator
      // method to examine each real character.
      TextElementEnumerator charEnum = StringInfo.GetTextElementEnumerator(s);
      while (charEnum.MoveNext()) {
         sb.AppendFormat(
           "Character at index {0} is '{1}'{2}",
           charEnum.ElementIndex, charEnum.GetTextElement(),
           Environment.NewLine);
      }

      // Show the results.
      Console.WriteLine("Result of GetTextElementEnumerator:");
      Console.WriteLine(sb);
   }

   // Show how to discover the index of each real character (honoring surrogates) in a string.
   static void EnumTextElementIndexes(String s) {
      // This StringBuilder holds the output results.
      StringBuilder sb = new StringBuilder();

      // Use the ParseCombiningCharacters method to
      // get the index of each real character in the string.
      Int32[] textElemIndex = StringInfo.ParseCombiningCharacters(s);

      // Iterate through each real character showing the character and the index where it was found.
      for (Int32 i = 0; i < textElemIndex.Length; i++) {
         sb.AppendFormat(
            "Character {0} starts at index {1}{2}",
            i, textElemIndex[i], Environment.NewLine);
      }

      // Show the results.
      Console.WriteLine("Result of ParseCombiningCharacters:");
      Console.WriteLine(sb);
   }
}

// This code produces the following output:
//
// Result of GetTextElementEnumerator:
// Character at index 0 is 'ā̈'
// Character at index 3 is 'b'
// Character at index 4 is 'ç'
//
// Result of ParseCombiningCharacters:
// Character 0 starts at index 0
// Character 1 starts at index 3
// Character 2 starts at index 4
Imports System.Text
Imports System.Globalization

Public Module Example
   Public Sub Main()
      ' The string below contains combining characters.
      Dim s As String = "a" + ChrW(&h0304) + ChrW(&h0308) + "bc" + ChrW(&h0327)

      ' Show each 'character' in the string.
      EnumTextElements(s)

      ' Show the index in the string where each 'character' starts.
      EnumTextElementIndexes(s)
   End Sub

   ' Show how to enumerate each real character (honoring surrogates) in a string.
   Sub EnumTextElements(s As String)
      ' This StringBuilder holds the output results.
      Dim sb As New StringBuilder()

      ' Use the enumerator returned from GetTextElementEnumerator 
      ' method to examine each real character.
      Dim charEnum As TextElementEnumerator = StringInfo.GetTextElementEnumerator(s)
      Do While charEnum.MoveNext()
         sb.AppendFormat("Character at index {0} is '{1}'{2}",
                         charEnum.ElementIndex, 
                         charEnum.GetTextElement(),
                         Environment.NewLine)
      Loop

      ' Show the results.
      Console.WriteLine("Result of GetTextElementEnumerator:")
      Console.WriteLine(sb)
   End Sub

   ' Show how to discover the index of each real character (honoring surrogates) in a string.
   Sub EnumTextElementIndexes(s As String)
      ' This StringBuilder holds the output results.
      Dim sb As New StringBuilder()

      ' Use the ParseCombiningCharacters method to 
      ' get the index of each real character in the string.
      Dim textElemIndex() As Integer = StringInfo.ParseCombiningCharacters(s)

      ' Iterate through each real character showing the character and the index where it was found.
      For i As Int32 = 0 To textElemIndex.Length - 1
         sb.AppendFormat("Character {0} starts at index {1}{2}",
                         i, textElemIndex(i), Environment.NewLine)
      Next

      ' Show the results.
      Console.WriteLine("Result of ParseCombiningCharacters:")
      Console.WriteLine(sb)
   End Sub
End Module
' The example displays the following output:
'
'       Result of GetTextElementEnumerator:
'       Character at index 0 is 'ā̈'
'       Character at index 3 is 'b'
'       Character at index 4 is 'ç'
'       
'       Result of ParseCombiningCharacters:
'       Character 0 starts at index 0
'       Character 1 starts at index 3
'       Character 2 starts at index 4

설명

.NET은 텍스트 요소를 단일 문자로 표시 되는 텍스트 단위 (즉, grapheme)로 정의 합니다. 텍스트 요소는 기본 문자, 서로게이트 쌍 또는 결합 문자 시퀀스가 될 수 있습니다. 유니코드 표준은 서로게이트 쌍을 두 코드 단위의 시퀀스로 구성 된 단일 추상 문자에 대 한 코딩 된 문자 표현으로 정의 합니다. 여기서 쌍의 첫 번째 단위는 상위 서로게이트이 고 두 번째는 하위 서로게이트입니다. 유니코드 표준은 조합 문자 시퀀스를 기본 문자와 하나 이상의 조합 문자 조합으로 정의 합니다. 서로게이트 쌍은 기본 문자 또는 결합 문자를 나타낼 수 있습니다.

텍스트 요소 열거자는 문자열의 데이터를 읽는 데만 사용 됩니다. 기본 문자열을 수정할 수 없습니다. 열거자에는 문자열에 대 한 단독 액세스 권한이 없습니다.

문자열의 첫 번째 텍스트 요소 앞 이나 문자열의 마지막 텍스트 요소 뒤에 위치 하는 경우 열거자가 잘못 된 상태입니다. 열거자의 상태가 잘못 된 경우를 호출 하면 Current 예외가 throw 됩니다.

처음에 열거자는 문자열의 첫 번째 텍스트 요소 앞에 배치 됩니다. Reset을 실행하면 열거자가 이 위치로 복원됩니다. 따라서 열거자가 만들어지거나를 호출한 후에는 Reset MoveNext 의 값을 읽기 전에를 호출 하 여 열거자를 문자열의 첫 번째 텍스트 요소로 이동 해야 합니다 Current .

Current에서는 MoveNext 또는 Reset이 호출될 때까지 동일한 개체를 반환합니다.

문자열의 끝이 전달 된 후 열거자는 다시 잘못 된 상태가 되 고를 호출 하면이 MoveNext 반환 됩니다 false . Current에 대 한 마지막 호출이 반환 되 면를 호출 하면 예외가 throw 됩니다 MoveNext false .

추가 정보

적용 대상

GetTextElementEnumerator(String, Int32)

지정된 인덱스에서 시작하여 문자열의 텍스트 요소를 반복하는 열거자를 반환합니다.

public:
 static System::Globalization::TextElementEnumerator ^ GetTextElementEnumerator(System::String ^ str, int index);
public static System.Globalization.TextElementEnumerator GetTextElementEnumerator (string str, int index);
static member GetTextElementEnumerator : string * int -> System.Globalization.TextElementEnumerator
Public Shared Function GetTextElementEnumerator (str As String, index As Integer) As TextElementEnumerator

매개 변수

str
String

반복할 문자열입니다.

index
Int32

반복을 시작할 인덱스(0부터 시작)입니다.

반환

TextElementEnumerator

index에서 시작하는 문자열에 대한 TextElementEnumerator입니다.

예외

str이(가) null인 경우

indexstr의 유효한 인덱스 범위를 벗어납니다.

설명

.NET은 텍스트 요소를 단일 문자로 표시 되는 텍스트 단위 (즉, grapheme)로 정의 합니다. 텍스트 요소는 기본 문자, 서로게이트 쌍 또는 결합 문자 시퀀스가 될 수 있습니다. 유니코드 표준은 서로게이트 쌍을 두 코드 단위의 시퀀스로 구성 된 단일 추상 문자에 대 한 코딩 된 문자 표현으로 정의 합니다. 여기서 쌍의 첫 번째 단위는 상위 서로게이트이 고 두 번째는 하위 서로게이트입니다. 유니코드 표준은 조합 문자 시퀀스를 기본 문자와 하나 이상의 조합 문자 조합으로 정의 합니다. 서로게이트 쌍은 기본 문자 또는 결합 문자를 나타낼 수 있습니다.

텍스트 요소 열거자는 문자열의 데이터를 읽는 데만 사용 됩니다. 기본 문자열을 수정할 수 없습니다. 열거자에는 문자열에 대 한 단독 액세스 권한이 없습니다.

문자열의 첫 번째 텍스트 요소 앞 이나 문자열의 마지막 텍스트 요소 뒤에 위치 하는 경우 열거자가 잘못 된 상태입니다. 열거자의 상태가 잘못 된 경우를 호출 하면 Current 예외가 throw 됩니다.

처음에 열거자는 문자열의 첫 번째 텍스트 요소 앞에 배치 됩니다. Reset을 실행하면 열거자가 이 위치로 복원됩니다. 따라서 열거자가 만들어지거나를 호출한 후에는 Reset MoveNext 의 값을 읽기 전에를 호출 하 여 열거자를 문자열의 첫 번째 텍스트 요소로 이동 해야 합니다 Current .

Current에서는 MoveNext 또는 Reset이 호출될 때까지 동일한 개체를 반환합니다.

문자열의 끝이 전달 된 후 열거자는 다시 잘못 된 상태가 되 고를 호출 하면이 MoveNext 반환 됩니다 false . Current에 대 한 마지막 호출이 반환 되 면를 호출 하면 예외가 throw 됩니다 MoveNext false .

추가 정보

적용 대상