TextElementEnumerator 类
枚举字符串的文本元素。
**命名空间:**System.Globalization
**程序集:**mscorlib(在 mscorlib.dll 中)
语法
声明
<SerializableAttribute> _
<ComVisibleAttribute(True)> _
Public Class TextElementEnumerator
Implements IEnumerator
用法
Dim instance As TextElementEnumerator
[SerializableAttribute]
[ComVisibleAttribute(true)]
public class TextElementEnumerator : IEnumerator
[SerializableAttribute]
[ComVisibleAttribute(true)]
public ref class TextElementEnumerator : IEnumerator
/** @attribute SerializableAttribute() */
/** @attribute ComVisibleAttribute(true) */
public class TextElementEnumerator implements IEnumerator
SerializableAttribute
ComVisibleAttribute(true)
public class TextElementEnumerator implements IEnumerator
备注
.NET Framework 将文本元素定义为显示为单个字符(即字素)的文本单元。文本元素可以是基字符、代理项对或组合字符序列。“Unicode 标准”将代理项对定义为单个抽象字符的编码的字符表示形式,该抽象字符包含一个由两个代码单元组成的序列,其中,第一个代码单元是高代理项,第二个是低代理项。“Unicode 标准”将组合字符序列定义为一个基字符与一个或多个组合字符的组合。代理项对可表示基字符或组合字符。有关代理项对和组合字符序列的更多信息,请参见 http://www.unicode.org 中的 Unicode Standard(Unicode 标准)。
文本元素枚举数只用于读取字符串中的数据。不能使用枚举数修改基础字符串。
枚举数对字符串的访问不是独占的。
创建枚举数时,它会制作字符串当前状态的快照。如果对字符串作了更改(如添加、修改或删除了文本元素),则快照将失去同步,并且枚举数会引发 InvalidOperationException。在同一时间从同一字符串创建的两个枚举数可有该字符串的不同快照。
如果枚举数位于字符串中第一个文本元素前或最后一个文本元素后,则该枚举数处于无效状态。每当枚举数处于无效状态时,调用 Current 会引发异常。
最初,枚举数位于字符串中第一个文本元素之前。Reset 也会使枚举数返回此位置。因此,在创建枚举数或调用 Reset 后,必须调用 MoveNext 将枚举数前移到字符串的第一个文本元素,然后才能读取 Current 的值。
在调用 MoveNext 或 Reset 之前,Current 返回同一对象。
超过字符串的结尾后,枚举数重新处于无效状态,调用 MoveNext 会返回 false。如果对 MoveNext 的最近一次调用返回 false,则调用 Current 会引发异常。
示例
下面的代码示例显示了 TextElementEnumerator 返回的值。
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]:
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]:
*/
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]:
*/
import System.*;
import System.Globalization.*;
public class SamplesTextElementEnumerator
{
public static void main(String[] args)
{
// 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}",
System.Convert.ToString(myTEE.get_ElementIndex()),
System.Convert.ToString(myTEE.get_Current()),
myTEE.GetTextElement());
}
} //main
} //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]:
*/
继承层次结构
System.Object
System.Globalization.TextElementEnumerator
线程安全
此类型的任何公共静态(Visual Basic 中的 Shared)成员都是线程安全的,但不保证所有实例成员都是线程安全的。
平台
Windows 98、Windows 2000 SP4、Windows CE、Windows Millennium Edition、Windows Mobile for Pocket PC、Windows Mobile for Smartphone、Windows Server 2003、Windows XP Media Center Edition、Windows XP Professional x64 Edition、Windows XP SP2、Windows XP Starter Edition
.NET Framework 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求。
版本信息
.NET Framework
受以下版本支持:2.0、1.1、1.0
.NET Compact Framework
受以下版本支持:2.0、1.0
请参见
参考
TextElementEnumerator 成员
System.Globalization 命名空间
System.Collections.IEnumerator