StringInfo.GetTextElementEnumerator 方法

定義

回傳一個枚舉器,遍歷字串的文字元素。

多載

名稱 Description
GetTextElementEnumerator(String)

回傳一個枚舉子,遍歷整個字串的文字元素。

GetTextElementEnumerator(String, Int32)

回傳一個枚舉器,從指定的索引開始遍歷字串的文字元素。

GetTextElementEnumerator(String)

回傳一個枚舉子,遍歷整個字串的文字元素。

public:
 static System::Globalization::TextElementEnumerator ^ GetTextElementEnumerator(System::String ^ str);
public static System.Globalization.TextElementEnumerator GetTextElementEnumerator(string str);
static member GetTextElementEnumerator : string -> System.Globalization.TextElementEnumerator
Public Shared Function GetTextElementEnumerator (str As String) As TextElementEnumerator

參數

str
String

要反覆處理的字串。

傳回

整串的A TextElementEnumerator

例外狀況

strnull

範例

下列範例示範呼叫 GetTextElementEnumerator 方法。 此範例是本類別更大 StringInfo 範例的一部分。

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 將文字元素定義為以單一字元顯示的文字單位,也就是字素。 文字元素可以是基底字元、替代字元對或組合字元序列。 Unicode 標準將代理對定義為單一抽象字元的編碼字元表示,該字元由兩個編碼單元組成,其中第一個單元為高代理,第二個為低代碼。 Unicode 標準將結合字元序列定義為由一個基礎字元與一個或多個組合字元組成。 替代角色對可以代表基礎角色或組合角色。

文字元素枚舉器僅用於讀取字串中的資料;它無法修改底層字串。 列舉器並未擁有對字串的專屬存取權。

如果列舉子位於字串的第一個文字元素之前或最後一個文字元素之後,則該枚舉器處於無效狀態。 當列舉器處於無效狀態時,呼叫 Current 會拋出例外。

最初,列舉器會位於字串的第一個文字元素之前。 Reset 同時也讓普查員回到這個位置。 因此,在建立 Reset 列舉器或呼叫後, MoveNext 必須先呼叫,將列舉器推進到字串的第一個文字元素,然後再讀取 的 Current值。

Current 回傳相同物件,直到被呼叫其中一 MoveNextReset

當字串結束後,列舉器再次處於無效狀態,呼叫 MoveNext 回傳 false。 若 Current 最後一次呼叫 MoveNext 返回 false,呼叫會拋出異常。

另請參閱

適用於

GetTextElementEnumerator(String, Int32)

回傳一個枚舉器,從指定的索引開始遍歷字串的文字元素。

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.TextElementEnumerator
Public Shared Function GetTextElementEnumerator (str As String, index As Integer) As TextElementEnumerator

參數

str
String

要反覆處理的字串。

index
Int32

從零為基礎的指數開始迭代。

傳回

TextElementEnumerator A 代表從 始index的字串。

例外狀況

strnull

index 不在 的 str有效索引範圍內。

備註

.NET 將文字元素定義為以單一字元顯示的文字單位,也就是字素。 文字元素可以是基底字元、替代字元對或組合字元序列。 Unicode 標準將代理對定義為單一抽象字元的編碼字元表示,該字元由兩個編碼單元組成,其中第一個單元為高代理,第二個為低代碼。 Unicode 標準將結合字元序列定義為由一個基礎字元與一個或多個組合字元組成。 替代角色對可以代表基礎角色或組合角色。

文字元素枚舉器僅用於讀取字串中的資料;它無法修改底層字串。 列舉器並未擁有對字串的專屬存取權。

如果列舉子位於字串的第一個文字元素之前或最後一個文字元素之後,則該枚舉器處於無效狀態。 當列舉器處於無效狀態時,呼叫 Current 會拋出例外。

最初,列舉器會位於字串的第一個文字元素之前。 Reset 同時也讓普查員回到這個位置。 因此,在建立 Reset 列舉器或呼叫後, MoveNext 必須先呼叫,將列舉器推進到字串的第一個文字元素,然後再讀取 的 Current值。

Current 回傳相同物件,直到被呼叫其中一 MoveNextReset

當字串結束後,列舉器再次處於無效狀態,呼叫 MoveNext 回傳 false。 若 Current 最後一次呼叫 MoveNext 返回 false,呼叫會拋出異常。

另請參閱

適用於