TextElementEnumerator 類別

定義

列舉字串的文字項目。Enumerates the text elements of a string.

public ref class TextElementEnumerator : System::Collections::IEnumerator
[System.Runtime.InteropServices.ComVisible(true)]
[System.Serializable]
public class TextElementEnumerator : System.Collections.IEnumerator
type TextElementEnumerator = class
    interface IEnumerator
Public Class TextElementEnumerator
Implements IEnumerator
繼承
TextElementEnumerator
屬性
實作

範例

下列範例會使用TextElementEnumerator類別來列舉字串的文字元素。The following example uses the TextElementEnumerator class to enumerate the text elements of a string.

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]:    a`      a`
[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]:    a`      a`
[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]:    a`      a`
'[4]:    Æ       Æ

備註

.NET Framework 會將文字元素定義為顯示為單一字元的文字單元, 也就是語素簇。The .NET Framework defines a text element as a unit of text that is displayed as a single character, that is, a grapheme. Text 元素可以是下列任何一項:A text element can be any of the following:

  • 以單一Char值表示的基底字元。A base character, which is represented as a single Char value. 例如, 拉丁大寫字母 A (U + 0041) 和拉丁小型字母 AE (U + 00E6) 是基底字元。For example, LATIN CAPITAL LETTER A (U+0041) and LATIN SMALL LETTER AE (U+00E6) are base characters.

  • 結合字元序列, 其中包含基底字元和一或多個合併字元。A combining character sequence, which consists of a base character and one or more combining characters. 例如, 範例中的拉丁大寫字母 A (U + 0041), 後面接著結合長音符 (U + 0304) 是組合字元序列。For example, example, LATIN CAPITAL LETTER A (U+0041) followed by COMBINING MACRON (U+0304) is a combining character sequence.

  • 代理字組, Unicode 標準會定義為單一抽象字元 (由兩個程式碼單位的序列所組成) 的編碼字元標記法: 高代理項和低代理項。Surrogate pairs, which the Unicode Standard defines as a coded character representation for a single abstract character that consists of a sequence of two code units: a high surrogate, and a low surrogate. 代理字組是用來將 Unicode 基本多語系平面以外的字元表示為 UTF-16 編碼的字元。Surrogate pairs are used to represent characters outside of the Unicode Basic Multilingual Plane as UTF-16 encoded characters. 例如, MT 字母 SAUIL (U + 10343) 是以 UTF-16 編碼表示, 其值為 0xD800, 而低代理項的值為0xDF43。For example, GOTHIC LETTER SAUIL (U+10343) is represented in UTF-16 encoding as a high surrogate whose value is 0xD800 and a low surrogate whose value is 0xDF43. 代理配對可以代表基底字元或結合字元。A surrogate pair can represent a base character or a combining character.

類別可讓您使用字串中的文字元素, 而不是使用單一Char物件。 TextElementEnumeratorThe TextElementEnumerator class allows you to work with the text elements in a string rather than with single Char objects.

您會將TextElementEnumerator字串傳遞StringInfo.GetTextElementEnumerator給方法, 以具現化代表特定字串的物件。You instantiate a TextElementEnumerator object that represents a particular string by passing the string to the StringInfo.GetTextElementEnumerator method. 這會傳回位於字串中第一個文字專案之前的列舉值。This returns an enumerator that is positioned before the first text element in the string. Reset呼叫方法也會將列舉值帶回此位置。Calling the Reset method also brings the enumerator back to this position. 因為這代表不正確狀態, 所以您必須MoveNext呼叫, 將列舉值前移至字串的第一個文字元素, 然後再讀取Current屬性的值, 以傳回目前的文字專案。Because this represents an invalid state, you must call MoveNext to advance the enumerator to the first text element of the string before reading the value of the Current property to return the current text element.

TextElementEnumerator使用物件時, 您必須負責定位列舉值。When working with a TextElementEnumerator object, you are responsible for positioning the enumerator. 屬性會傳回相同的文字元素, 直到您MoveNext呼叫或ResetCurrentThe Current property returns the same text element until you call either MoveNext or Reset. 如果列舉值位於第一個文字專案之前, 或在字串中的最後一個文字專案之後, 就會處於無效狀態。The enumerator is in an invalid state if it is positioned before the first text element or after the last text element in the string. 當列舉值處於無效狀態時, 嘗試取出Current屬性的值會擲回例外狀況。When the enumerator is in an invalid state, attempting to retrieve the value of the Current property throws an exception. 您可以藉由測試MoveNext屬性的傳回值是否為, false判斷枚舉器是否處於無效狀態。You can determine whether the enumerator is in an invalid state by testing whether the return value of the MoveNext property is false.

物件代表在TextElementEnumerator物件具現化時, 字串變數或字串常值的目前狀態快照集。 TextElementEnumeratorThe TextElementEnumerator object represents a snapshot of the current state of a string variable or string literal at the moment that the TextElementEnumerator object is instantiated. 請注意:Note that:

  • 文字元素枚舉器只能用來讀取字串中的資料。Text element enumerators can only be used to read data in a string. 它們無法修改基礎字串。They cannot modify the underlying string.

  • 列舉值對它所代表的字串沒有獨佔存取權。An enumerator does not have exclusive access to the string that it represents. 建立列舉值之後, 可以修改字串變數。A string variable can be modified after the enumerator is created.

  • 物件會列舉在物件具現化時, 出現在字串中的文字元素。 TextElementEnumerator TextElementEnumeratorA TextElementEnumerator object enumerates the text elements present in the string at the time that the TextElementEnumerator object was instantiated. 如果之後修改該變數, 則不會反映字串變數的任何後續變更。It does not reflect any subsequent changes to the string variable if that variable is modified afterward.

  • 因為類別不會覆寫Object.Equals, 所以TextElementEnumerator表示相同字串的兩個物件將視為不相等。 TextElementEnumeratorBecause the TextElementEnumerator class does not override Object.Equals, two TextElementEnumerator objects that represent the same string will be considered unequal.

屬性

Current

取得字串中目前的文字項目。Gets the current text element in the string.

ElementIndex

取得列舉值目前所位在的文字項目的索引。Gets the index of the text element that the enumerator is currently positioned over.

方法

Equals(Object)

判斷指定的物件是否等於目前的物件。Determines whether the specified object is equal to the current object.

(繼承來源 Object)
GetHashCode()

作為預設雜湊函數。Serves as the default hash function.

(繼承來源 Object)
GetTextElement()

取得字串中目前的文字項目。Gets the current text element in the string.

GetType()

取得目前執行個體的 TypeGets the Type of the current instance.

(繼承來源 Object)
MemberwiseClone()

建立目前 Object 的淺層複製。Creates a shallow copy of the current Object.

(繼承來源 Object)
MoveNext()

將列舉值前移至字串的下一個文字項目。Advances the enumerator to the next text element of the string.

Reset()

設定列舉值至它的初始位置,這是在字串中第一個文字項目之前。Sets the enumerator to its initial position, which is before the first text element in the string.

ToString()

傳回代表目前物件的字串。Returns a string that represents the current object.

(繼承來源 Object)

適用於

另請參閱