Export (0) Print
Expand All

TextElementEnumerator Class

Enumerates the text elements of a string.

For a list of all members of this type, see TextElementEnumerator Members.

System.Object
   System.Globalization.TextElementEnumerator

[Visual Basic]
<Serializable>
Public Class TextElementEnumerator
   Implements IEnumerator
[C#]
[Serializable]
public class TextElementEnumerator : IEnumerator
[C++]
[Serializable]
public __gc class TextElementEnumerator : public IEnumerator
[JScript]
public
   Serializable
class TextElementEnumerator implements IEnumerator

Thread Safety

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

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 a base character, a surrogate pair, or a combining character sequence. The Unicode Standard defines a surrogate pair as a coded character representation for a single abstract character that consists of a sequence of two code units, where the first unit of the pair is a high-surrogate and the second is a low-surrogate. The Unicode Standard defines a combining character sequence as a combination of a base character and one or more combining characters. A surrogate pair can represent a base character or a combining character. For more information on surrogate pairs and combining character sequences, see The Unicode Standard at http://www.unicode.org.

Text element enumerators are intended to be used only to read data in the string. Enumerators cannot be used to modify the underlying string.

The enumerator does not have exclusive access to the string.

When an enumerator is created, it takes a snapshot of the current state of the string. If changes are made to the string, such as adding, modifying, or deleting text elements, the snapshot gets out of sync and the enumerator throws an InvalidOperationException. Two enumerators created from the same string at the same time can have different snapshots of the string.

The enumerator is in an invalid state if it is positioned before the first text element in the string or after the last text element in the string. Whenever the enumerator is in an invalid state, calling Current throws an exception.

Initially, the enumerator is positioned before the first text element in the string. Reset also brings the enumerator back to this position. Therefore, after an enumerator is created or after a Reset, MoveNext must be called to advance the enumerator to the first text element of the string before reading the value of Current.

Current returns the same object until either MoveNext or Reset is called.

After the end of the string is passed, the enumerator is again in an invalid state and calling MoveNext returns false. Calling Current throws an exception if the last call to MoveNext returned false.

Example

[Visual Basic, C#, C++] The following code example shows the values returned by TextElementEnumerator.

[Visual Basic] 
Imports System
Imports System.Globalization
Imports Microsoft.VisualBasic

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 'Main 

End Class 'SamplesTextElementEnumerator

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


[C#] 
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]:           

*/

[C++] 
#using <mscorlib.dll>
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 = S"\xD800\xDC00" S"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(S"Index\tCurrent\tGetTextElement");
   myTEE -> Reset();
   while (myTEE -> MoveNext())
      Console::WriteLine(S"[{0}]:\t {1}\t {2}", __box(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]:           

*/

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Namespace: System.Globalization

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework

Assembly: Mscorlib (in Mscorlib.dll)

See Also

TextElementEnumerator Members | System.Globalization Namespace | System.Collections.IEnumerator

Show:
© 2014 Microsoft