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

例外

strnullです。

次の例では、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 では、テキスト要素を 1 つの文字 (つまり、グラフ化) として表示されるテキストの単位として定義します。 テキスト要素には、基本文字、サロゲート ペア、または組み合わせ文字シーケンスを指定できます。 Unicode 標準では、2 つのコード単位のシーケンスで構成される 1 つの抽象文字のコード化された文字表現としてサロゲート ペアが定義されています。この場合、ペアの最初の単位は高サロゲート、2 番目のユニットは低サロゲートです。 Unicode 標準では、基本文字と 1 つ以上の組み合わせ文字の組み合わせとして、結合文字シーケンスが定義されます。 サロゲート ペアは、基本文字または結合文字を表します。

text 要素列挙子は、文字列内のデータを読み取る場合にのみ使用されます。基になる文字列を変更することはできません。 列挙子には、文字列への排他アクセス権が含まれます。

列挙子が、文字列内の最初のテキスト要素の前、または文字列内の最後のテキスト要素の後に配置されている場合、列挙子は無効な状態になります。 列挙子が無効な状態の場合、 を呼び出すると Current 例外がスローされます。

最初に、列挙子は文字列内の最初のテキスト要素の前に配置されます。 また、Reset メソッドは、列挙子を最初の位置に戻します。 したがって、列挙子を作成した後、または を呼び出した後に を呼び出して、列挙子を文字列の最初のテキスト要素に進めてから の値を読み取 Reset MoveNext る必要があります Current

Current は、MoveNext または Reset が呼び出されるまでは同じオブジェクトを返します。

文字列の末尾が渡された後、列挙子は無効な状態に戻り、 を呼び出して MoveNext を返します false 。 前回 Current を呼び出して 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

例外

strnullです。

indexstr の有効なインデックスの範囲外です。

注釈

.NET では、テキスト要素を 1 つの文字 (つまり、グラフ化) として表示されるテキストの単位として定義します。 テキスト要素には、基本文字、サロゲート ペア、または組み合わせ文字シーケンスを指定できます。 Unicode 標準では、2 つのコード単位のシーケンスで構成される 1 つの抽象文字のコード化された文字表現としてサロゲート ペアが定義されています。この場合、ペアの最初の単位は高サロゲート、2 番目のユニットは低サロゲートです。 Unicode 標準では、基本文字と 1 つ以上の組み合わせ文字の組み合わせとして、結合文字シーケンスが定義されます。 サロゲート ペアは、基本文字または結合文字を表します。

text 要素列挙子は、文字列内のデータを読み取る場合にのみ使用されます。基になる文字列を変更することはできません。 列挙子には、文字列への排他アクセス権が含まれます。

列挙子が、文字列内の最初のテキスト要素の前、または文字列内の最後のテキスト要素の後に配置されている場合、列挙子は無効な状態になります。 列挙子が無効な状態の場合、 を呼び出すると Current 例外がスローされます。

最初に、列挙子は文字列内の最初のテキスト要素の前に配置されます。 また、Reset メソッドは、列挙子を最初の位置に戻します。 したがって、列挙子を作成した後、または を呼び出した後に を呼び出して、列挙子を文字列の最初のテキスト要素に進めてから の値を読み取 Reset MoveNext る必要があります Current

Current は、MoveNext または Reset が呼び出されるまでは同じオブジェクトを返します。

文字列の末尾が渡された後、列挙子は無効な状態に戻り、 を呼び出して MoveNext を返します false 。 前回 Current を呼び出して MoveNext が返された後に false を呼び出すと、例外がスローされます。

こちらもご覧ください

適用対象