Rediger

Del via


TextElementEnumerator Class

Definition

Enumerates the text elements of a string.

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
Inheritance
TextElementEnumerator
Attributes
Implements

Examples

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]:    à       à
[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]:    Æ       Æ

Remarks

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:

  • A base character, which is represented as a single Char value. 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. For example, example, LATIN CAPITAL LETTER A (U+0041) followed by COMBINING MACRON (U+0304) is a combining character sequence.

  • 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. Surrogate pairs are used to represent characters outside of the Unicode Basic Multilingual Plane as UTF-16 encoded characters. 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.

The TextElementEnumerator class allows you to work with the text elements in a string rather than with single Char objects.

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. Calling the Reset method also brings the enumerator back to this position. 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.

When working with a TextElementEnumerator object, you are responsible for positioning the enumerator. The 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. When the enumerator is in an invalid state, attempting to retrieve the value of the Current property throws an exception. You can determine whether the enumerator is in an invalid state by testing whether the return value of the MoveNext property is false.

The 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.

  • A 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.

  • Because the TextElementEnumerator class does not override Object.Equals, two TextElementEnumerator objects that represent the same string will be considered unequal.

Properties

Current

Gets the current text element in the string.

ElementIndex

Gets the index of the text element that the enumerator is currently positioned over.

Methods

Equals(Object)

Determines whether the specified object is equal to the current object.

(Inherited from Object)
GetHashCode()

Serves as the default hash function.

(Inherited from Object)
GetTextElement()

Gets the current text element in the string.

GetType()

Gets the Type of the current instance.

(Inherited from Object)
MemberwiseClone()

Creates a shallow copy of the current Object.

(Inherited from 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.

(Inherited from Object)

Applies to

See also