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 namespace System;
using namespace System::Globalization;
int 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 S"a" followed by the combining grave accent)
   //   - a base character (the ligature S"")
   String^ myString = L"\xD800\xDC00"
   L"a\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]:    Æ       Æ

*/
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會將文字元素定義為顯示為單一字元的文字單位,也就是 grapheme。 文字元素可以是下列任一項:

  • 以單 Char 一值表示的基底字元。 例如,拉丁大寫字母 A (U+0041) 和拉丁小寫字母 AE (U+00E6) 都是基底字元。

  • 結合字元序列,由基底字元和一或多個結合字元組成。 例如,拉丁大寫字母 A (U+0041) 後面接著結合 MACRON (U+0304) 是合併字元序列。

  • Unicode Standard所定義的 Surrogate 配對是單一抽象字元的編碼字元標記法,其中包含兩個程式碼單位的序列:高 Surrogate 和低 Surrogate。 Surrogate 配對可用來將 Unicode 基本多語平面以外的字元表示為 UTF-16 編碼字元。 例如,在 UTF-) 16 編碼中, (會以 UTF-16 編碼表示為0XD800,而低 Surrogate 的值為 0xDF43。 Surrogate 配對可以代表基底字元或合併字元。

類別 TextElementEnumerator 可讓您使用字串中的文字元素,而不是使用單一 Char 物件。

您可以藉由將字串傳遞至 StringInfo.GetTextElementEnumerator 方法,具現化 TextElementEnumerator 代表特定字串的 物件。 這會傳回位在字串中第一個文字元素之前的列舉值。 Reset呼叫 方法也會將列舉值帶回這個位置。 因為這代表不正確狀態,所以您必須呼叫 MoveNext 將列舉值前進到字串的第一個文字元素,然後再讀取 屬性的值 Current ,才能傳回目前的文字專案。

使用 TextElementEnumerator 物件時,您必須負責定位列舉值。 屬性 Current 會傳回相同的文字專案,直到您呼叫 MoveNextReset 為止。 如果列舉值位於第一個文字專案之前或字串中最後一個文字元素之後,則列舉值處於無效狀態。 當列舉值處於無效狀態時,嘗試擷取 Current 屬性值會擲回例外狀況。 您可以測試屬性的 MoveNext 傳回值是否為 false ,判斷列舉值是否處於無效狀態。

物件 TextElementEnumerator 代表物件具現化時字串變數或字串常值目前狀態的 TextElementEnumerator 快照集。 請注意:

  • Text 元素列舉值只能用來讀取字串中的資料。 它們無法修改基礎字串。

  • 列舉值沒有它所代表之字串的獨佔存取權。 建立列舉值之後,即可修改字串變數。

  • TextElementEnumerator物件會在物件具現化時 TextElementEnumerator ,列舉字串中存在的文字元素。 如果之後修改該變數,它不會反映字串變數的任何後續變更。

  • 因為 類別 TextElementEnumerator 不會覆寫 Object.Equals ,所以代表相同字串的兩 TextElementEnumerator 個物件會被視為不相等。

屬性

Current

取得字串中目前的文字項目。

ElementIndex

取得列舉值目前所位在的文字項目的索引。

方法

Equals(Object)

判斷指定的物件是否等於目前的物件。

(繼承來源 Object)
GetHashCode()

做為預設雜湊函式。

(繼承來源 Object)
GetTextElement()

取得字串中目前的文字項目。

GetType()

取得目前執行個體的 Type

(繼承來源 Object)
MemberwiseClone()

建立目前 Object 的淺層複製。

(繼承來源 Object)
MoveNext()

將列舉值前移至字串的下一個文字項目。

Reset()

設定列舉值至它的初始位置,這是在字串中第一個文字項目之前。

ToString()

傳回代表目前物件的字串。

(繼承來源 Object)

適用於

另請參閱