StringInfo.GetTextElementEnumerator StringInfo.GetTextElementEnumerator StringInfo.GetTextElementEnumerator StringInfo.GetTextElementEnumerator Method

定義

文字列のテキスト要素を反復処理する列挙子を返します。Returns an enumerator that iterates through the text elements of a string.

オーバーロード

GetTextElementEnumerator(String) GetTextElementEnumerator(String) GetTextElementEnumerator(String) GetTextElementEnumerator(String)

文字列全体のテキスト要素を反復処理する列挙子を返します。Returns an enumerator that iterates through the text elements of the entire string.

GetTextElementEnumerator(String, Int32) GetTextElementEnumerator(String, Int32) GetTextElementEnumerator(String, Int32) GetTextElementEnumerator(String, Int32)

指定したインデックスから開始する文字列のテキスト要素を反復処理する列挙子を返します。Returns an enumerator that iterates through the text elements of the string, starting at the specified index.

GetTextElementEnumerator(String) GetTextElementEnumerator(String) GetTextElementEnumerator(String) GetTextElementEnumerator(String)

文字列全体のテキスト要素を反復処理する列挙子を返します。Returns an enumerator that iterates through the text elements of the entire 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 String String String

反復処理対象の文字列。The string to iterate through.

戻り値

例外

次の例では、GetTextElementEnumerator メソッドを呼び出す方法を示しています。The following example demonstrates calling the GetTextElementEnumerator method. この例は、 StringInfoクラスに対して提供される大規模な例の一部です。This example is part of a larger example provided for the StringInfo class.

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 '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
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 '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

注釈

.NET では、テキスト要素を1つの文字 (書記素) として表示されるテキストの単位として定義します。.NET defines a text element as a unit of text that is displayed as a single character, that is, a grapheme. テキスト要素には、基本文字、サロゲートペア、または組み合わせ文字シーケンスを指定できます。A text element can be a base character, a surrogate pair, or a combining character sequence. Unicode 規格では、2つのコード単位のシーケンスで構成される単一の抽象文字のコード化された文字表現としてサロゲートペアを定義します。ペアの最初の単位は上位サロゲート、2番目は下位サロゲートです。The Unicode Standard defines a surrogate pair as a coded character representation for a single abstract character that consists of a sequence of two code units, where the first unit of the pair is a high surrogate and the second is a low surrogate. Unicode 規格では、基本文字と1つ以上の組み合わせ文字の組み合わせとして、組み合わせ文字シーケンスを定義します。The Unicode Standard defines a combining character sequence as a combination of a base character and one or more combining characters. サロゲートペアは、基本文字または組み合わせ文字を表すことができます。A surrogate pair can represent a base character or a combining character.

Text 要素列挙子は、文字列内のデータを読み取るためにのみ使用されます。基になる文字列を変更することはできません。The text element enumerator is used only to read data in the string; it cannot modify the underlying string. 列挙子には、文字列への排他アクセス権はありません。The enumerator does not have exclusive access to the string.

列挙子は、文字列の最初のテキスト要素の前、または文字列内の最後のテキスト要素の後に配置されている場合、無効な状態になります。The enumerator is in an invalid state if it is positioned before the first text element in the string or after the last text element in the string. 列挙子が無効な状態にある場合、 Currentを呼び出すと例外がスローされます。When the enumerator is in an invalid state, calling Current throws an exception.

最初に、列挙子は文字列の最初のテキスト要素の前に配置されます。Initially, the enumerator is positioned before the first text element in the string. Reset を実行した場合も、列挙子はこの位置に戻されます。Reset also brings the enumerator back to this position. したがって、列挙子が作成されResetた後、 MoveNextまたはが呼び出された後、のCurrent値を読み取る前に、を呼び出して、列挙子を文字列の最初のテキスト要素に進めておく必要があります。Therefore, after an enumerator is created or after Reset is called, MoveNext must be called to advance the enumerator to the first text element of the string before reading the value of Current.

Current は、MoveNext または Reset が呼び出されるまでは同じオブジェクトを返します。Current returns the same object until either MoveNext or Reset is called.

文字列の末尾が渡されると、列挙子はもう一度無効な状態になり、 MoveNextfalse呼び出すと戻ります。After the end of the string is passed, the enumerator is again in an invalid state and calling MoveNext returns false. Current最後の呼び出しでが返さfalseれたMoveNext場合、を呼び出すと例外がスローされます。Calling Current throws an exception if the last call to MoveNext returned false.

こちらもご覧ください

GetTextElementEnumerator(String, Int32) GetTextElementEnumerator(String, Int32) GetTextElementEnumerator(String, Int32) GetTextElementEnumerator(String, Int32)

指定したインデックスから開始する文字列のテキスト要素を反復処理する列挙子を返します。Returns an enumerator that iterates through the text elements of the string, starting at the specified index.

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 String String String

反復処理対象の文字列。The string to iterate through.

index
Int32 Int32 Int32 Int32

反復処理を開始する位置の、0 から始まるインデックス番号。The zero-based index at which to start iterating.

戻り値

例外

indexstr の有効なインデックスの範囲外です。index is outside the range of valid indexes for str.

注釈

.NET では、テキスト要素を1つの文字 (書記素) として表示されるテキストの単位として定義します。.NET defines a text element as a unit of text that is displayed as a single character, that is, a grapheme. テキスト要素には、基本文字、サロゲートペア、または組み合わせ文字シーケンスを指定できます。A text element can be a base character, a surrogate pair, or a combining character sequence. Unicode 規格では、2つのコード単位のシーケンスで構成される単一の抽象文字のコード化された文字表現としてサロゲートペアを定義します。ペアの最初の単位は上位サロゲート、2番目は下位サロゲートです。The Unicode Standard defines a surrogate pair as a coded character representation for a single abstract character that consists of a sequence of two code units, where the first unit of the pair is a high surrogate and the second is a low surrogate. Unicode 規格では、基本文字と1つ以上の組み合わせ文字の組み合わせとして、組み合わせ文字シーケンスを定義します。The Unicode Standard defines a combining character sequence as a combination of a base character and one or more combining characters. サロゲートペアは、基本文字または組み合わせ文字を表すことができます。A surrogate pair can represent a base character or a combining character.

Text 要素列挙子は、文字列内のデータを読み取るためにのみ使用されます。基になる文字列を変更することはできません。The text element enumerator is used only to read data in the string; it cannot modify the underlying string. 列挙子には、文字列への排他アクセス権はありません。The enumerator does not have exclusive access to the string.

列挙子は、文字列の最初のテキスト要素の前、または文字列内の最後のテキスト要素の後に配置されている場合、無効な状態になります。The enumerator is in an invalid state if it is positioned before the first text element in the string or after the last text element in the string. 列挙子が無効な状態にある場合、 Currentを呼び出すと例外がスローされます。When the enumerator is in an invalid state, calling Current throws an exception.

最初に、列挙子は文字列の最初のテキスト要素の前に配置されます。Initially, the enumerator is positioned before the first text element in the string. Reset を実行した場合も、列挙子はこの位置に戻されます。Reset also brings the enumerator back to this position. したがって、列挙子が作成されResetた後、 MoveNextまたはが呼び出された後、のCurrent値を読み取る前に、を呼び出して、列挙子を文字列の最初のテキスト要素に進めておく必要があります。Therefore, after an enumerator is created or after Reset is called, MoveNext must be called to advance the enumerator to the first text element of the string before reading the value of Current.

Current は、MoveNext または Reset が呼び出されるまでは同じオブジェクトを返します。Current returns the same object until either MoveNext or Reset is called.

文字列の末尾が渡されると、列挙子はもう一度無効な状態になり、 MoveNextfalse呼び出すと戻ります。After the end of the string is passed, the enumerator is again in an invalid state and calling MoveNext returns false. Current最後の呼び出しでが返さfalseれたMoveNext場合、を呼び出すと例外がスローされます。Calling Current throws an exception if the last call to MoveNext returned false.

こちらもご覧ください

適用対象