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. 文本元素可以是以下任一项: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. 例如, 哥特字母 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. Current 调用或Reset之前, 属性将返回相同的文本元素。 MoveNextThe 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()

返回一个表示当前对象的 string。Returns a string that represents the current object.

(继承自 Object)

适用于

另请参阅