StringInfo.GetTextElementEnumerator 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
傳回可以逐一查看字串文字項目的列舉值。
多載
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
要逐一查看的字串。
傳回
整個字串的 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。 文字元素可以是基底字元、Surrogate 配對或合併字元序列。 Unicode Standard 會將 Surrogate 配對定義為單一抽象字元的編碼字元表示法,其中包含兩個程式碼單位的序列,其中配對的第一個單位是高 Surrogate,而第二個則是低 Surrogate。 Unicode 標準會將結合字元序列定義為基底字元和一或多個結合字元的組合。 Surrogate 配對可以代表基底字元或合併字元。
text 元素列舉值僅用於讀取字串中的數據;它無法修改基礎字串。 列舉值沒有字串的獨佔存取權。
如果列舉值位於字串中的第一個文字元素之前或字串中最後一個文字元素之後,則列舉值的狀態無效。 當列舉值處於無效狀態時,呼叫 Current 會擲回例外狀況。
一開始,列舉值會放在字串中的第一個文字元素之前。 Reset 也會將列舉值帶回至這個位置。 因此,在建立列舉值或 Reset 呼叫 之後,必須先呼叫 , MoveNext 才能將列舉值前進到字串的第一個文字元素,再讀取 的值 Current。
Current 會傳回相同的物件直到呼叫 MoveNext 或 Reset。
傳遞字串結尾之後,列舉值會再次處於無效的狀態,而呼叫 MoveNext 會 false
傳回 。 如果上次呼叫傳回 ,則呼叫CurrentMoveNext會擲回例外狀況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
要從該處開始反覆查看之以零起始的索引。
傳回
起始於 index
之字串的 TextElementEnumerator。
例外狀況
str
為 null
。
index
超出 str
的有效索引範圍。
備註
.NET 會將文字元素定義為顯示為單一字元的文字單位,也就是 grapheme。 文字元素可以是基底字元、Surrogate 配對或合併字元序列。 Unicode Standard 會將 Surrogate 配對定義為單一抽象字元的編碼字元表示法,其中包含兩個程式碼單位的序列,其中配對的第一個單位是高 Surrogate,而第二個則是低 Surrogate。 Unicode 標準會將結合字元序列定義為基底字元和一或多個結合字元的組合。 Surrogate 配對可以代表基底字元或合併字元。
text 元素列舉值僅用於讀取字串中的數據;它無法修改基礎字串。 列舉值沒有字串的獨佔存取權。
如果列舉值位於字串中的第一個文字元素之前或字串中最後一個文字元素之後,則列舉值的狀態無效。 當列舉值處於無效狀態時,呼叫 Current 會擲回例外狀況。
一開始,列舉值會放在字串中的第一個文字元素之前。 Reset 也會將列舉值帶回至這個位置。 因此,在建立列舉值或 Reset 呼叫 之後,必須先呼叫 , MoveNext 才能將列舉值前進到字串的第一個文字元素,再讀取 的值 Current。
Current 會傳回相同的物件直到呼叫 MoveNext 或 Reset。
傳遞字串結尾之後,列舉值會再次處於無效的狀態,而呼叫 MoveNext 會 false
傳回 。 如果上次呼叫傳回 ,則呼叫CurrentMoveNext會擲回例外狀況false
。