StringInfo.GetTextElementEnumerator 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
返回一个枚举器,该枚举器循环访问字符串的文本元素。
重载
| 名称 | 说明 |
|---|---|
| GetTextElementEnumerator(String) |
返回一个枚举器,该枚举器循环访问整个字符串的文本元素。 |
| GetTextElementEnumerator(String, Int32) |
返回从指定索引开始循环访问字符串的文本元素的枚举数。 |
GetTextElementEnumerator(String)
- Source:
- StringInfo.cs
- Source:
- StringInfo.cs
- 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.TextElementEnumerator
Public Shared Function GetTextElementEnumerator (str As String) As TextElementEnumerator
参数
- str
- String
要循环访问的字符串。
返回
整个字符串的 A TextElementEnumerator 。
例外
str 是 null。
示例
以下示例演示如何调用 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。 如果最后一次调用返回false,则调用CurrentMoveNext将引发异常。
另请参阅
适用于
GetTextElementEnumerator(String, Int32)
- Source:
- StringInfo.cs
- Source:
- StringInfo.cs
- 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.TextElementEnumerator
Public Shared Function GetTextElementEnumerator (str As String, index As Integer) As TextElementEnumerator
参数
- str
- String
要循环访问的字符串。
- index
- Int32
从零开始迭代的索引。
返回
从 .index开始的字符串的 ATextElementEnumerator。
例外
str 是 null。
index 超出了有效索引 str的范围。
注解
.NET 将文本元素定义为显示为单个字符(即图形体)的文本单元。 文本元素可以是基字符、代理项对或组合字符序列。 Unicode 标准将代理项对定义为由两个代码单元组成的单个抽象字符的编码字符表示形式,其中第一个代理项是高代理项,第二个是低代理项。 Unicode 标准将组合字符序列定义为基字符和一个或多个组合字符的组合。 代理项对可以表示基字符或组合字符。
文本元素枚举器仅用于读取字符串中的数据;它无法修改基础字符串。 枚举器对字符串没有独占访问权限。
如果枚举器位于字符串中的第一个文本元素之前或字符串中最后一个文本元素之后,则枚举器处于无效状态。 当枚举器处于无效状态时,调用 Current 将引发异常。
最初,枚举器位于字符串中的第一个文本元素之前。 Reset 还将枚举器带回此位置。 因此,在创建枚举器或 Reset 调用后, MoveNext 必须先调用枚举器,才能在读取值 Current之前将枚举器前进到字符串的第一个文本元素。
Current返回相同的对象,直到调用或MoveNextReset调用。
传递字符串末尾后,枚举器再次处于无效状态并调用 MoveNext 返回 false。 如果最后一次调用返回false,则调用CurrentMoveNext将引发异常。