StringInfo.GetTextElementEnumerator 方法     
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
返回一个循环访问字符串的文本元素的枚举器。
重载
| GetTextElementEnumerator(String) | 返回一个循环访问整个字符串的文本元素的枚举数。 | 
| GetTextElementEnumerator(String, Int32) | 返回一个枚举数,它循环访问字符串的文本元素并从指定索引处开始。 | 
GetTextElementEnumerator(String)
- Source:
- StringInfo.cs
- Source:
- StringInfo.cs
- Source:
- StringInfo.cs
返回一个循环访问整个字符串的文本元素的枚举数。
public:
 static System::Globalization::TextElementEnumerator ^ GetTextElementEnumerator(System::String ^ str);public static System.Globalization.TextElementEnumerator GetTextElementEnumerator (string str);static member GetTextElementEnumerator : string -> System.Globalization.TextElementEnumeratorPublic Shared Function GetTextElementEnumerator (str As String) As TextElementEnumerator参数
- str
- String
要循环访问的字符串。
返回
整个字符串的 TextElementEnumerator。
例外
              str 为 null。
示例
下面的示例演示如何调用 GetTextElementEnumerator 方法。 此示例是为 类提供的更大示例的 StringInfo 一部分。
using namespace System;
using namespace System::Text;
using namespace System::Globalization;
// Show how to enumerate each real character (honoring surrogates)
// in a string.
void EnumTextElements(String^ combiningChars)
{
    // This StringBuilder holds the output results.
    StringBuilder^ sb = gcnew StringBuilder();
    // Use the enumerator returned from GetTextElementEnumerator
    // method to examine each real character.
    TextElementEnumerator^ charEnum =
        StringInfo::GetTextElementEnumerator(combiningChars);
    while (charEnum->MoveNext())
    {
        sb->AppendFormat("Character at index {0} is '{1}'{2}", 
            charEnum->ElementIndex, charEnum->GetTextElement(), 
            Environment::NewLine);
    }
    // Show the results.
    Console::WriteLine("Result of GetTextElementEnumerator:");
    Console::WriteLine(sb);
}
// Show how to discover the index of each real character
// (honoring surrogates) in a string.
void EnumTextElementIndexes(String^ combiningChars)
{
    // This StringBuilder holds the output results.
    StringBuilder^ sb = gcnew StringBuilder();
    // Use the ParseCombiningCharacters method to
    // get the index of each real character in the string.
    array <int>^ textElemIndex =
        StringInfo::ParseCombiningCharacters(combiningChars);
    // Iterate through each real character showing the character
    // and the index where it was found.
    for (int i = 0; i < textElemIndex->Length; i++)
    {
        sb->AppendFormat("Character {0} starts at index {1}{2}",
            i, textElemIndex[i], Environment::NewLine);
    }
    // Show the results.
    Console::WriteLine("Result of ParseCombiningCharacters:");
    Console::WriteLine(sb);
}
int main()
{
    // The string below contains combining characters.
    String^ combiningChars = L"a\u0304\u0308bc\u0327";
    // Show each 'character' in the string.
    EnumTextElements(combiningChars);
    // Show the index in the string where each 'character' starts.
    EnumTextElementIndexes(combiningChars);
};
// This code produces the following output.
//
// Result of GetTextElementEnumerator:
// Character at index 0 is 'a-"'
// Character at index 3 is 'b'
// Character at index 4 is 'c,'
//
// Result of ParseCombiningCharacters:
// Character 0 starts at index 0
// Character 1 starts at index 3
// Character 2 starts at index 4
using System;
using System.Text;
using System.Globalization;
public sealed class App {
   static void Main() {
      // The string below contains combining characters.
      String s = "a\u0304\u0308bc\u0327";
      // Show each 'character' in the string.
      EnumTextElements(s);
      // Show the index in the string where each 'character' starts.
      EnumTextElementIndexes(s);
   }
   // Show how to enumerate each real character (honoring surrogates) in a string.
   static void EnumTextElements(String s) {
      // This StringBuilder holds the output results.
      StringBuilder sb = new StringBuilder();
      // Use the enumerator returned from GetTextElementEnumerator
      // method to examine each real character.
      TextElementEnumerator charEnum = StringInfo.GetTextElementEnumerator(s);
      while (charEnum.MoveNext()) {
         sb.AppendFormat(
           "Character at index {0} is '{1}'{2}",
           charEnum.ElementIndex, charEnum.GetTextElement(),
           Environment.NewLine);
      }
      // Show the results.
      Console.WriteLine("Result of GetTextElementEnumerator:");
      Console.WriteLine(sb);
   }
   // Show how to discover the index of each real character (honoring surrogates) in a string.
   static void EnumTextElementIndexes(String s) {
      // This StringBuilder holds the output results.
      StringBuilder sb = new StringBuilder();
      // Use the ParseCombiningCharacters method to
      // get the index of each real character in the string.
      Int32[] textElemIndex = StringInfo.ParseCombiningCharacters(s);
      // Iterate through each real character showing the character and the index where it was found.
      for (Int32 i = 0; i < textElemIndex.Length; i++) {
         sb.AppendFormat(
            "Character {0} starts at index {1}{2}",
            i, textElemIndex[i], Environment.NewLine);
      }
      // Show the results.
      Console.WriteLine("Result of ParseCombiningCharacters:");
      Console.WriteLine(sb);
   }
}
// This code produces the following output:
//
// Result of GetTextElementEnumerator:
// Character at index 0 is 'ā̈'
// Character at index 3 is 'b'
// Character at index 4 is 'ç'
//
// Result of ParseCombiningCharacters:
// Character 0 starts at index 0
// Character 1 starts at index 3
// Character 2 starts at index 4
Imports System.Text
Imports System.Globalization
Public Module Example
   Public Sub Main()
      ' The string below contains combining characters.
      Dim s As String = "a" + ChrW(&h0304) + ChrW(&h0308) + "bc" + ChrW(&h0327)
      ' Show each 'character' in the string.
      EnumTextElements(s)
      ' Show the index in the string where each 'character' starts.
      EnumTextElementIndexes(s)
   End Sub
   ' Show how to enumerate each real character (honoring surrogates) in a string.
   Sub EnumTextElements(s As String)
      ' This StringBuilder holds the output results.
      Dim sb As New StringBuilder()
      ' Use the enumerator returned from GetTextElementEnumerator 
      ' method to examine each real character.
      Dim charEnum As TextElementEnumerator = StringInfo.GetTextElementEnumerator(s)
      Do While charEnum.MoveNext()
         sb.AppendFormat("Character at index {0} is '{1}'{2}",
                         charEnum.ElementIndex, 
                         charEnum.GetTextElement(),
                         Environment.NewLine)
      Loop
      ' Show the results.
      Console.WriteLine("Result of GetTextElementEnumerator:")
      Console.WriteLine(sb)
   End Sub
   ' Show how to discover the index of each real character (honoring surrogates) in a string.
   Sub EnumTextElementIndexes(s As String)
      ' This StringBuilder holds the output results.
      Dim sb As New StringBuilder()
      ' Use the ParseCombiningCharacters method to 
      ' get the index of each real character in the string.
      Dim textElemIndex() As Integer = StringInfo.ParseCombiningCharacters(s)
      ' Iterate through each real character showing the character and the index where it was found.
      For i As Int32 = 0 To textElemIndex.Length - 1
         sb.AppendFormat("Character {0} starts at index {1}{2}",
                         i, textElemIndex(i), Environment.NewLine)
      Next
      ' Show the results.
      Console.WriteLine("Result of ParseCombiningCharacters:")
      Console.WriteLine(sb)
   End Sub
End Module
' The example displays the following output:
'
'       Result of GetTextElementEnumerator:
'       Character at index 0 is 'ā̈'
'       Character at index 3 is 'b'
'       Character at index 4 is 'ç'
'       
'       Result of ParseCombiningCharacters:
'       Character 0 starts at index 0
'       Character 1 starts at index 3
'       Character 2 starts at index 4
注解
.NET 将文本元素定义为显示为单个字符(即 grapheme)的文本单元。 文本元素可以是基字符、代理项对或组合字符序列。 Unicode Standard 将代理项对定义为单个抽象字符的编码字符表示形式,该抽象字符由两个代码单元组成的序列组成,其中对的第一个单元是高代理项,第二个是低代理项。 Unicode 标准将组合字符序列定义为基字符和一个或多个组合字符的组合。 代理项对可以表示基字符或组合字符。
文本元素枚举器仅用于读取字符串中的数据;它不能修改基础字符串。 枚举器对字符串没有独占访问权限。
如果枚举器位于字符串中的第一个文本元素之前或字符串中最后一个文本元素之后,则枚举器处于无效状态。 当枚举器处于无效状态时,调用 Current 将引发异常。
最初,枚举器位于字符串中的第一个文本元素之前。 Reset 也会将枚举器放回此位置。 因此,在创建枚举器或调用 枚举器后 Reset , MoveNext 必须调用 以将枚举器推进到字符串的第一个文本元素,然后再读取 的值 Current。
在调用 Current 或 MoveNext 之前,Reset 返回同一对象。
传递字符串末尾后,枚举器再次处于无效状态,调用 MoveNext 将 false返回 。 
              Current如果最后一次调用返回 ,false则调用 MoveNext 将引发异常。
另请参阅
适用于
GetTextElementEnumerator(String, Int32)
- Source:
- StringInfo.cs
- Source:
- StringInfo.cs
- Source:
- StringInfo.cs
返回一个枚举数,它循环访问字符串的文本元素并从指定索引处开始。
public:
 static System::Globalization::TextElementEnumerator ^ GetTextElementEnumerator(System::String ^ str, int index);public static System.Globalization.TextElementEnumerator GetTextElementEnumerator (string str, int index);static member GetTextElementEnumerator : string * int -> System.Globalization.TextElementEnumeratorPublic Shared Function GetTextElementEnumerator (str As String, index As Integer) As TextElementEnumerator参数
- str
- String
要循环访问的字符串。
- index
- Int32
开始迭代处的从零开始的索引。
返回
在 index 处开始的字符串的 TextElementEnumerator。
例外
              str 为 null。
              index 超出了 str 的有效索引范围。
注解
.NET 将文本元素定义为显示为单个字符(即 grapheme)的文本单元。 文本元素可以是基字符、代理项对或组合字符序列。 Unicode Standard 将代理项对定义为单个抽象字符的编码字符表示形式,该抽象字符由两个代码单元组成的序列组成,其中对的第一个单元是高代理项,第二个是低代理项。 Unicode 标准将组合字符序列定义为基字符和一个或多个组合字符的组合。 代理项对可以表示基字符或组合字符。
文本元素枚举器仅用于读取字符串中的数据;它不能修改基础字符串。 枚举器对字符串没有独占访问权限。
如果枚举器位于字符串中的第一个文本元素之前或字符串中最后一个文本元素之后,则枚举器处于无效状态。 当枚举器处于无效状态时,调用 Current 将引发异常。
最初,枚举器位于字符串中的第一个文本元素之前。 Reset 也会将枚举器放回此位置。 因此,在创建枚举器或调用 枚举器后 Reset , MoveNext 必须调用 以将枚举器推进到字符串的第一个文本元素,然后再读取 的值 Current。
在调用 Current 或 MoveNext 之前,Reset 返回同一对象。
传递字符串末尾后,枚举器再次处于无效状态,调用 MoveNext 将 false返回 。 
              Current如果最后一次调用返回 ,false则调用 MoveNext 将引发异常。