次の方法で共有


TextElementEnumerator クラス

定義

文字列のテキスト要素を列挙します。

public ref class TextElementEnumerator : System::Collections::IEnumerator
public class TextElementEnumerator : System.Collections.IEnumerator
[System.Serializable]
public class TextElementEnumerator : System.Collections.IEnumerator
[System.Serializable]
[System.Runtime.InteropServices.ComVisible(true)]
public class TextElementEnumerator : System.Collections.IEnumerator
type TextElementEnumerator = class
    interface IEnumerator
[<System.Serializable>]
type TextElementEnumerator = class
    interface IEnumerator
[<System.Serializable>]
[<System.Runtime.InteropServices.ComVisible(true)>]
type TextElementEnumerator = class
    interface IEnumerator
Public Class TextElementEnumerator
Implements IEnumerator
継承
TextElementEnumerator
属性
実装

次の例では、 TextElementEnumerator クラスを使用して文字列のテキスト要素を列挙します。

using System;
using System.Globalization;

public class SamplesTextElementEnumerator  {

   public static void Main()  {

      // Creates and initializes a String containing the following:
      //   - a surrogate pair (high surrogate U+D800 and low surrogate U+DC00)
      //   - a combining character sequence (the Latin small letter "a" followed by the combining grave accent)
      //   - a base character (the ligature "")
      String myString = "\uD800\uDC00\u0061\u0300\u00C6";

      // Creates and initializes a TextElementEnumerator for myString.
      TextElementEnumerator myTEE = StringInfo.GetTextElementEnumerator( myString );

      // Displays the values returned by ElementIndex, Current and GetTextElement.
      // Current and GetTextElement return a string containing the entire text element.
      Console.WriteLine( "Index\tCurrent\tGetTextElement" );
      myTEE.Reset();
      while (myTEE.MoveNext())  {
         Console.WriteLine( "[{0}]:\t{1}\t{2}", myTEE.ElementIndex, myTEE.Current, myTEE.GetTextElement() );
      }
   }
}

/*
This code produces the following output.  The question marks take the place of high and low surrogates.

Index   Current GetTextElement
[0]:    𐀀       𐀀
[2]:    à       à
[4]:    Æ       Æ

*/
Imports System.Globalization

Public Class SamplesTextElementEnumerator

   Public Shared Sub Main()

      ' Creates and initializes a String containing the following:
      '   - a surrogate pair (high surrogate U+D800 and low surrogate U+DC00)
      '   - a combining character sequence (the Latin small letter "a" followed by the combining grave accent)
      '   - a base character (the ligature "")
      Dim myString As String = ChrW(&HD800) & ChrW(&HDC00) & ChrW(&H0061) & ChrW(&H0300) & ChrW(&H00C6)

      ' Creates and initializes a TextElementEnumerator for myString.
      Dim myTEE As TextElementEnumerator = StringInfo.GetTextElementEnumerator( myString )

      ' Displays the values returned by ElementIndex, Current and GetTextElement.
      ' Current and GetTextElement return a string containing the entire text element.
      Console.WriteLine("Index" + ControlChars.Tab + "Current" + ControlChars.Tab + "GetTextElement")
      myTEE.Reset()
      While myTEE.MoveNext()
         Console.WriteLine("[{0}]:" + ControlChars.Tab + "{1}" + ControlChars.Tab + "{2}", myTEE.ElementIndex, myTEE.Current, myTEE.GetTextElement())
      End While

   End Sub

End Class

'This code produces the following output.  The question marks take the place of high and low surrogates.
'
'Index   Current GetTextElement
'[0]:    𐀀       𐀀
'[2]:    à       à
'[4]:    Æ       Æ

注釈

.NET Framework では、テキスト要素を、単一の文字 (つまりグラフ) として表示されるテキストの単位として定義します。 テキスト要素には、次のいずれかを指定できます。

  • 1 つの Char 値として表される基本文字。 たとえば、ラテン大文字 A (U+0041) とラテン小文字 AE (U+00E6) は基本文字です。

  • 基本文字と 1 つ以上の結合文字で構成される結合文字シーケンス。 たとえば、ラテン大文字 A (U+0041) の後に COMBINING MACRON (U+0304) が続く文字シーケンスは結合文字シーケンスです。

  • サロゲート ペア。 Unicode 標準 では、高サロゲートと低サロゲートという 2 つのコード単位のシーケンスで構成される 1 つの抽象文字のコード化された文字表現として定義されます。 サロゲート ペアは、Unicode Basic 多言語プレーンの外部の文字を UTF-16 でエンコードされた文字として表すために使用されます。 たとえば、GOTHIC LETTER SAUIL (U+10343) は UTF-16 エンコードで、値が0xD800で、値が0xDF43低いサロゲートとして表されます。 サロゲート ペアは、基本文字または結合文字を表すことができます。

TextElementEnumerator クラスを使用すると、単一のChar オブジェクトではなく、文字列内のテキスト要素を操作できます。

StringInfo.GetTextElementEnumerator メソッドに文字列を渡すことで、特定の文字列を表すTextElementEnumerator オブジェクトをインスタンス化します。 これは、文字列内の最初のテキスト要素の前に配置されている列挙子を返します。 Reset メソッドを呼び出すと、列挙子もこの位置に戻ります。 これは無効な状態を表しているため、現在のテキスト要素を返すCurrent プロパティの値を読み取る前に、列挙子を文字列の最初のテキスト要素に進めるためにMoveNextを呼び出す必要があります。

TextElementEnumerator オブジェクトを操作する場合は、列挙子を配置する必要があります。 Current プロパティは、MoveNextまたはResetを呼び出すまで、同じテキスト要素を返します。 列挙子が最初のテキスト要素の前または文字列内の最後のテキスト要素の後に配置されている場合、列挙子は無効な状態になります。 列挙子が無効な状態の場合、 Current プロパティの値を取得しようとすると、例外がスローされます。 列挙子が無効な状態にあるかどうかを確認するには、 MoveNext() プロパティの戻り値が falseされているかどうかをテストします。

TextElementEnumerator オブジェクトは、TextElementEnumerator オブジェクトがインスタンス化された時点での文字列変数または文字列リテラルの現在の状態のスナップショットを表します。 次の点に注意してください。

  • テキスト要素列挙子は、文字列内のデータの読み取りにのみ使用できます。 基になる文字列を変更することはできません。

  • 列挙子は、それが表す文字列への排他的アクセス権を持っていません。 文字列変数は、列挙子の作成後に変更できます。

  • TextElementEnumerator オブジェクトは、TextElementEnumerator オブジェクトがインスタンス化された時点で文字列に存在するテキスト要素を列挙します。 その変数が後で変更された場合、文字列変数に対する後続の変更は反映されません。

  • TextElementEnumerator クラスはObject.Equalsをオーバーライドしないため、同じ文字列を表す 2 つのTextElementEnumerator オブジェクトは等しくないと見なされます。

プロパティ

名前 説明
Current

文字列内の現在のテキスト要素を取得します。

ElementIndex

列挙子が現在配置されているテキスト要素のインデックスを取得します。

メソッド

名前 説明
Equals(Object)

指定したオブジェクトが現在のオブジェクトと等しいかどうかを判断します。

(継承元 Object)
GetHashCode()

既定のハッシュ関数として機能します。

(継承元 Object)
GetTextElement()

文字列内の現在のテキスト要素を取得します。

GetType()

現在のインスタンスの Type を取得します。

(継承元 Object)
MemberwiseClone()

現在の Objectの簡易コピーを作成します。

(継承元 Object)
MoveNext()

列挙子を文字列の次のテキスト要素に進めます。

Reset()

列挙子を最初の位置 (文字列の最初のテキスト要素の前) に設定します。

ToString()

現在のオブジェクトを表す文字列を返します。

(継承元 Object)

適用対象

こちらもご覧ください