StringInfo.GetTextElementEnumerator 方法

定義

傳回可以逐一查看字串文字項目的列舉值。Returns an enumerator that iterates through the text elements of a string.

多載

GetTextElementEnumerator(String)

傳回可以逐一查看整個字串文字項目的列舉值。Returns an enumerator that iterates through the text elements of the entire string.

GetTextElementEnumerator(String, Int32)

傳回可以從指定索引處開始來逐一查看字串文字項目的列舉值。Returns an enumerator that iterates through the text elements of the string, starting at the specified index.

GetTextElementEnumerator(String)

傳回可以逐一查看整個字串文字項目的列舉值。Returns an enumerator that iterates through the text elements of the entire 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

要逐一查看的字串。The string to iterate through.

傳回

整個字串的 TextElementEnumeratorA TextElementEnumerator for the entire string.

例外狀況

strnullstr is null.

範例

下列程式碼範例示範如何呼叫 GetTextElementEnumerator 方法。The following example demonstrates calling the GetTextElementEnumerator method. 這個範例是針對 StringInfo 類別提供的較大範例的一部分。This example is part of a larger example provided for the StringInfo class.

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

備註

.NET 會將文字元素定義為顯示為單一字元的文字單元,也就是語素簇。.NET defines a text element as a unit of text that is displayed as a single character, that is, a grapheme. Text 元素可以是基底字元、代理字組或結合字元序列。A text element can be a base character, a surrogate pair, or a combining character sequence. Unicode 標準會將代理組定義為單一抽象字元(由兩個程式碼單位的序列所組成)的編碼字元標記法,其中配對的第一個單位是高代理,而第二個是低代理。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. Unicode 標準會將結合字元序列定義為基底字元和一或多個合併字元的組合。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.

Text 元素列舉值只能用來讀取字串中的資料;它無法修改基礎字串。The text element enumerator is used only to read data in the string; it cannot modify the underlying string. 列舉值沒有字串的獨佔存取權。The enumerator does not have exclusive access to 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. 當列舉值處於無效狀態時,呼叫 Current 會擲回例外狀況。When 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 也會將列舉值帶回至這個位置。Reset also brings the enumerator back to this position. 因此,在建立枚舉器或呼叫 Reset 之後,必須呼叫 MoveNext,將列舉值前移至字串的第一個文字元素,然後再讀取 Current的值。Therefore, after an enumerator is created or after Reset is called, MoveNext must be called to advance the enumerator to the first text element of the string before reading the value of Current.

Current 會傳回相同的物件直到呼叫 MoveNextResetCurrent returns the same object until either MoveNext or Reset is called.

在傳遞字串結尾之後,列舉值會再次處於無效狀態,而且呼叫 MoveNext 會傳回 falseAfter the end of the string is passed, the enumerator is again in an invalid state and calling MoveNext returns false. 如果最後一次呼叫 MoveNext 傳回 false,則呼叫 Current 會擲回例外狀況。Calling Current throws an exception if the last call to MoveNext returned false.

另請參閱

GetTextElementEnumerator(String, Int32)

傳回可以從指定索引處開始來逐一查看字串文字項目的列舉值。Returns an enumerator that iterates through the text elements of the string, starting at the specified index.

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

要逐一查看的字串。The string to iterate through.

index
Int32

要從該處開始反覆查看之以零起始的索引。The zero-based index at which to start iterating.

傳回

起始於 index 之字串的 TextElementEnumeratorA TextElementEnumerator for the string starting at index.

例外狀況

strnullstr is null.

index 超出 str 的有效索引範圍。index is outside the range of valid indexes for str.

備註

.NET 會將文字元素定義為顯示為單一字元的文字單元,也就是語素簇。.NET defines a text element as a unit of text that is displayed as a single character, that is, a grapheme. Text 元素可以是基底字元、代理字組或結合字元序列。A text element can be a base character, a surrogate pair, or a combining character sequence. Unicode 標準會將代理組定義為單一抽象字元(由兩個程式碼單位的序列所組成)的編碼字元標記法,其中配對的第一個單位是高代理,而第二個是低代理。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. Unicode 標準會將結合字元序列定義為基底字元和一或多個合併字元的組合。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.

Text 元素列舉值只能用來讀取字串中的資料;它無法修改基礎字串。The text element enumerator is used only to read data in the string; it cannot modify the underlying string. 列舉值沒有字串的獨佔存取權。The enumerator does not have exclusive access to 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. 當列舉值處於無效狀態時,呼叫 Current 會擲回例外狀況。When 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 也會將列舉值帶回至這個位置。Reset also brings the enumerator back to this position. 因此,在建立枚舉器或呼叫 Reset 之後,必須呼叫 MoveNext,將列舉值前移至字串的第一個文字元素,然後再讀取 Current的值。Therefore, after an enumerator is created or after Reset is called, MoveNext must be called to advance the enumerator to the first text element of the string before reading the value of Current.

Current 會傳回相同的物件直到呼叫 MoveNextResetCurrent returns the same object until either MoveNext or Reset is called.

在傳遞字串結尾之後,列舉值會再次處於無效狀態,而且呼叫 MoveNext 會傳回 falseAfter the end of the string is passed, the enumerator is again in an invalid state and calling MoveNext returns false. 如果最後一次呼叫 MoveNext 傳回 false,則呼叫 Current 會擲回例外狀況。Calling Current throws an exception if the last call to MoveNext returned false.

另請參閱

適用於