StringInfo.GetTextElementEnumerator メソッド

定義

文字列のテキスト要素を反復処理する列挙子を返します。

オーバーロード

GetTextElementEnumerator(String)

文字列全体のテキスト要素を反復処理する列挙子を返します。

GetTextElementEnumerator(String, Int32)

指定したインデックスから開始する文字列のテキスト要素を反復処理する列挙子を返します。

GetTextElementEnumerator(String)

ソース:
StringInfo.cs
ソース:
StringInfo.cs
ソース:
StringInfo.cs

文字列全体のテキスト要素を反復処理する列挙子を返します。

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

例外

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

テキスト要素列挙子は、文字列内のデータを読み取るためにのみ使用されます。基になる文字列を変更することはできません。 列挙子は、文字列に対する排他的アクセス権を持っていません。

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

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

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

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

こちらもご覧ください

適用対象

GetTextElementEnumerator(String, Int32)

ソース:
StringInfo.cs
ソース:
StringInfo.cs
ソース:
StringInfo.cs

指定したインデックスから開始する文字列のテキスト要素を反復処理する列挙子を返します。

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 から始まるインデックス番号。

戻り値

index から開始する文字列の TextElementEnumerator

例外

strnullです。

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

注釈

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

テキスト要素列挙子は、文字列内のデータを読み取るためにのみ使用されます。基になる文字列を変更することはできません。 列挙子は、文字列に対する排他的アクセス権を持っていません。

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

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

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

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

こちらもご覧ください

適用対象