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将文本元素定义为显示为单个字符的文本单元,即图形体。 文本元素可以是下列任一元素:

  • 一个基字符,表示为单个 Char 值。 例如,拉丁文大写字母 A (U+0041) 和拉丁文小写字母 AE (U+00E6) 是基字符。

  • 组合字符序列,由基字符和一个或多个组合字符组成。 例如,拉丁文大写字母 A (U+0041) 后跟 COMBINING MACRON (U+0304) 是组合字符序列。

  • Unicode Standard 将代理项对定义为单个抽象字符的编码字符表示形式,其中包含两个代码单元的序列:高代理项和低代理项。 代理项对用于将 Unicode 基本多语言平面之外的字符表示为 UTF-16 编码字符。 例如,哥特文字母 SAUIL (U+10343) 以 UTF-16 编码表示为高代理项,其值为0xD800,其值为0xDF43的代理项。 代理项对可以表示基字符或组合字符。

TextElementEnumerator 允许你处理字符串中的文本元素,而不是使用单个 Char 对象。

通过将字符串传递给方法来实例化 TextElementEnumerator 表示特定字符串 StringInfo.GetTextElementEnumerator 的对象。 这会返回一个枚举器,该枚举数位于字符串中的第一个文本元素之前。 Reset调用该方法还会将枚举器带回此位置。 由于这表示无效状态,因此在读取属性的值Current以返回当前文本元素之前,必须调用MoveNext将枚举器推进到字符串的第一个文本元素。

使用 TextElementEnumerator 对象时,你负责定位枚举器。 属性Current返回相同的文本元素,直到调用或MoveNextReset调用 。 如果枚举器位于第一个文本元素之前或字符串中最后一个文本元素之后,则其处于无效状态。 当枚举器处于无效状态时,尝试检索属性的值 Current 将引发异常。 可以通过测试属性的 MoveNext 返回值是否为无效状态来确定枚举器是否处于 false无效状态。

TextElementEnumerator 对象表示实例化对象时 TextElementEnumerator 字符串变量或字符串文本当前状态的快照。 请注意:

  • 文本元素枚举器只能用于读取字符串中的数据。 它们无法修改基础字符串。

  • 枚举器对它所表示的字符串没有独占访问权限。 创建枚举器后,可以修改字符串变量。

  • 对象 TextElementEnumerator 枚举实例化对象时 TextElementEnumerator 字符串中存在的文本元素。 如果之后修改了该变量,则它不会反映对字符串变量的任何后续更改。

  • TextElementEnumerator由于该类不重写Object.Equals,因此表示同一字符串的两TextElementEnumerator个对象将被视为不相等。

属性

Current

获取字符串中的当前文本元素。

ElementIndex

获取枚举数当前置于其上的文本元素的索引。

方法

Equals(Object)

确定指定对象是否等于当前对象。

(继承自 Object)
GetHashCode()

作为默认哈希函数。

(继承自 Object)
GetTextElement()

获取字符串中的当前文本元素。

GetType()

获取当前实例的 Type

(继承自 Object)
MemberwiseClone()

创建当前 Object 的浅表副本。

(继承自 Object)
MoveNext()

将枚举数前移到字符串的下一个文本元素。

Reset()

将枚举数设置为其初始位置,该位置位于字符串中第一个文本元素之前。

ToString()

返回表示当前对象的字符串。

(继承自 Object)

适用于

另请参阅