Export (0) Print
Expand All

TextElementEnumerator Class

Enumerates the text elements of a string.

System.Object
  System.Globalization.TextElementEnumerator

Namespace:  System.Globalization
Assemblies:   mscorlib (in mscorlib.dll)
  System.Globalization (in System.Globalization.dll)

[SerializableAttribute]
[ComVisibleAttribute(true)]
public class TextElementEnumerator : IEnumerator

The TextElementEnumerator type exposes the following members.

  NameDescription
Public propertySupported by the XNA FrameworkSupported by Portable Class LibrarySupported in .NET for Windows Store appsCurrentGets the current text element in the string.
Public propertySupported by the XNA FrameworkSupported by Portable Class LibrarySupported in .NET for Windows Store appsElementIndexGets the index of the text element that the enumerator is currently positioned over.
Top

  NameDescription
Public methodSupported by the XNA FrameworkSupported by Portable Class LibrarySupported in .NET for Windows Store appsEquals(Object)Determines whether the specified object is equal to the current object. (Inherited from Object.)
Protected methodSupported by the XNA FrameworkSupported by Portable Class LibrarySupported in .NET for Windows Store appsFinalizeAllows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object.)
Public methodSupported by the XNA FrameworkSupported by Portable Class LibrarySupported in .NET for Windows Store appsGetHashCodeServes as the default hash function. (Inherited from Object.)
Public methodSupported by the XNA FrameworkSupported by Portable Class LibrarySupported in .NET for Windows Store appsGetTextElementGets the current text element in the string.
Public methodSupported by the XNA FrameworkSupported by Portable Class LibrarySupported in .NET for Windows Store appsGetTypeGets the Type of the current instance. (Inherited from Object.)
Protected methodSupported by the XNA FrameworkSupported by Portable Class LibrarySupported in .NET for Windows Store appsMemberwiseCloneCreates a shallow copy of the current Object. (Inherited from Object.)
Public methodSupported by the XNA FrameworkSupported by Portable Class LibrarySupported in .NET for Windows Store appsMoveNextAdvances the enumerator to the next text element of the string.
Public methodSupported by the XNA FrameworkSupported by Portable Class LibrarySupported in .NET for Windows Store appsResetSets the enumerator to its initial position, which is before the first text element in the string.
Public methodSupported by the XNA FrameworkSupported by Portable Class LibrarySupported in .NET for Windows Store appsToStringReturns a string that represents the current object. (Inherited from Object.)
Top

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-8 encoded characters. For example, GOTHIC LETTER SAUIL (U+10343) is represented in UTF-8 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.

The following example uses the TextElementEnumerator class to enumerate the text elements of a string.

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

*/

.NET Framework

Supported in: 4.6, 4.5, 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Portable Class Library

Supported in: Portable Class Library

.NET for Windows Store apps

Supported in: Windows 8

Supported in: Windows Phone 8.1

Supported in: Windows Phone Silverlight 8.1

Supported in: Windows Phone Silverlight 8

Windows Phone 8.1, Windows Phone 8, Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
Show:
© 2014 Microsoft