CharEnumerator 类

定义

支持循环访问 String 对象并读取其各个字符。Supports iterating over a String object and reading its individual characters. 无法继承此类。This class cannot be inherited.

public ref class CharEnumerator sealed : ICloneable, System::Collections::Generic::IEnumerator<char>
[System.Runtime.InteropServices.ComVisible(true)]
[System.Serializable]
public sealed class CharEnumerator : ICloneable, System.Collections.Generic.IEnumerator<char>
type CharEnumerator = class
    interface IEnumerator
    interface ICloneable
    interface IEnumerator<char>
    interface IDisposable
Public NotInheritable Class CharEnumerator
Implements ICloneable, IEnumerator(Of Char)
继承
CharEnumerator
属性
实现

示例

下面的示例使用 CharEnumerator 类来枚举字符串中的单个字符。The following example uses the CharEnumerator class to enumerate the individual characters in a string. 它通过调用 String.GetEnumerator 方法实例化 CharEnumerator 对象,通过调用 MoveNext 方法从一个字符移动到下一个字符,并通过检索 Current 属性的值显示当前字符。It instantiates a CharEnumerator object by calling the String.GetEnumerator method, moves from one character to the next by calling the MoveNext method, and displays the current character by retrieving the value of the Current property.

String ^ title = "A Tale of Two Cities";
CharEnumerator ^ chEnum = title->GetEnumerator();
int ctr = 1;
String ^ outputLine1 = nullptr;
String ^ outputLine2 = nullptr;
String ^ outputLine3 = nullptr; 

while (chEnum->MoveNext())
{
   outputLine1 += ctr < 10 || ctr % 10 != 0 ? "  " : (ctr / 10) + " ";
   outputLine2 += (ctr % 10) + " ";
   outputLine3 += chEnum->Current + " ";
   ctr++;
}

Console::WriteLine("The length of the string is {0} characters:", 
                  title->Length);
Console::WriteLine(outputLine1);
Console::WriteLine(outputLine2);    
Console::WriteLine(outputLine3);
// The example displays the following output to the console:      
//       The length of the string is 20 characters:
//                         1                   2
//       1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0
//       A   T a l e   o f   T w o   C i t i e s
string title = "A Tale of Two Cities";
CharEnumerator chEnum = title.GetEnumerator();
int ctr = 1;
string outputLine1 = null;
string outputLine2 = null;
string outputLine3 = null; 

while (chEnum.MoveNext())
{
   outputLine1 += ctr < 10 || ctr % 10 != 0 ? "  " : (ctr / 10) + " ";
   outputLine2 += (ctr % 10) + " ";
   outputLine3 += chEnum.Current + " ";
   ctr++;
}

Console.WriteLine("The length of the string is {0} characters:", 
                  title.Length);
Console.WriteLine(outputLine1);
Console.WriteLine(outputLine2);    
Console.WriteLine(outputLine3);
// The example displays the following output to the console:      
//       The length of the string is 20 characters:
//                         1                   2
//       1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0
//       A   T a l e   o f   T w o   C i t i e s
Dim title As String = "A Tale of Two Cities"
Dim chEnum As CharEnumerator = title.GetEnumerator()
Dim ctr As Integer = 1
Dim outputLine1, outputLine2, outputLine3 As String 

Do While chEnum.MoveNext()
   outputLine1 += CStr(iif(ctr < 10 Or ctr Mod 10 <> 0, "  ", CStr(ctr \ 10) + " ")) 
   outputLine2 += (ctr Mod 10)& " "
   outputLine3 += chEnum.Current & " "
   ctr += 1
Loop

Console.WriteLine("The length of the string is {0} characters:", _
                  title.Length)
Console.WriteLine(outputLine1)
Console.WriteLine(outputLine2)    
Console.WriteLine(outputLine3)
' The example displays the following output to the console:      
'       The length of the string is 20 characters:
'                         1                   2
'       1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0
'       A   T a l e   o f   T w o   C i t i e s

但请注意,可以使用 foreach (在中C#)或 For Each (在 Visual Basic 中)以更直观的方式执行相同的操作,如下面的示例所示。Note, however, that the same operation can be performed somewhat more intuitively by using foreach (in C#) or For Each (in Visual Basic), as the following example shows.

String ^ title = "A Tale of Two Cities";
int ctr = 1;
String ^ outputLine1 = nullptr;
String ^ outputLine2 = nullptr;
String ^ outputLine3 = nullptr; 

for each (wchar_t ch in title)
{
   outputLine1 += ctr < 10 || ctr % 10 != 0 ? "  " : (ctr / 10) + " ";
   outputLine2 += (ctr % 10) + " ";
   outputLine3 += ch + " ";
   ctr++;
}

Console::WriteLine("The length of the string is {0} characters:", 
                  title->Length);
Console::WriteLine(outputLine1);
Console::WriteLine(outputLine2);    
Console::WriteLine(outputLine3);
// The example displays the following output to the console:      
//       The length of the string is 20 characters:
//                         1                   2
//       1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0
//       A   T a l e   o f   T w o   C i t i e s
string title = "A Tale of Two Cities";
int ctr = 1;
string outputLine1 = null;
string outputLine2 = null;
string outputLine3 = null; 

foreach (char ch in title)
{
   outputLine1 += ctr < 10 || ctr % 10 != 0 ? "  " : (ctr / 10) + " ";
   outputLine2 += (ctr % 10) + " ";
   outputLine3 += ch + " ";
   ctr++;
}

Console.WriteLine("The length of the string is {0} characters:", 
                  title.Length);
Console.WriteLine(outputLine1);
Console.WriteLine(outputLine2);    
Console.WriteLine(outputLine3);
// The example displays the following output to the console:      
//       The length of the string is 20 characters:
//                         1                   2
//       1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0
//       A   T a l e   o f   T w o   C i t i e s
Dim title As String = "A Tale of Two Cities"
Dim ctr As Integer = 1
Dim outputLine1, outputLine2, outputLine3 As String 

For Each ch As Char In title
   outputLine1 += CStr(iif(ctr < 10 Or ctr Mod 10 <> 0, "  ", CStr(ctr \ 10) + " ")) 
   outputLine2 += (ctr Mod 10)& " "
   outputLine3 += ch & " "
   ctr += 1
Next

Console.WriteLine("The length of the string is {0} characters:", _
                  title.Length)
Console.WriteLine(outputLine1)
Console.WriteLine(outputLine2)    
Console.WriteLine(outputLine3)
' The example displays the following output to the console:      
'       The length of the string is 20 characters:
'                         1                   2
'       1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0
'       A   T a l e   o f   T w o   C i t i e s

注解

CharEnumerator 提供对引用的 String 对象中的字符的只读访问。A CharEnumerator provides read-only access to the characters in a referenced String object. 例如,Microsoft Visual Basic 和C#编程语言的 foreach 语句,这些语言循环访问集合的元素,从 String 对象检索 CharEnumerator,以便循环访问该对象中的字符。For example, the foreach statement of the Microsoft Visual Basic and C# programming languages, which iterates through the elements of a collection, retrieves a CharEnumerator from a String object in order to iterate through the characters in that object.

重要

CharEnumerator 类枚举单个16位 Char 实例。The CharEnumerator class enumerates individual 16-bit Char instances. 它不会将 graphemes (即,后跟一个或多个 combiding 字符的字符)或代理项对(即 Unicode 基本多语言平面之外的字符)视为单个字符。It does not consider graphemes (that is, a character followed by one or more combiding characters) or surrogate pairs (that is, characters outside the Unicode Basic Multilingual Plane) as single characters. 对于将这些类型的字符作为单个单元进行处理的枚举数,请使用 StringInfo 类。For an enumerator that handles these types of characters as a single unit, use the StringInfo class.

CharEnumerator没有公共构造函数。There is no public constructor for CharEnumerator. 相反,请调用 String 对象的 GetEnumerator 方法来获取初始化为引用该字符串的 CharEnumeratorInstead, call a String object's GetEnumerator method to obtain a CharEnumerator that is initialized to reference the string.

CharEnumerator 维护 CharEnumerator 引用字符串中字符的内部索引。A CharEnumerator maintains an internal index to the characters in the string the CharEnumerator references. 当索引引用字符串中第一个字符之前或最后一个字符之后的字符位置时,该索引的状态无效,在引用字符串中的字符时有效。The state of the index is invalid when it references a character position logically before the first character or after the last character in the string, and valid when it references a character within the string. 索引初始化为逻辑上位于第一个字符之前的位置,并在迭代完成后设置为最后一个字符之后的位置。The index is initialized to a position logically before the first character, and is set to a position after the last character when the iteration is complete. 如果尝试在索引无效时访问字符,则会引发异常。An exception is thrown if you attempt to access a character while the index is invalid.

MoveNext 方法将索引递增一,因此依次访问第一个和后面的字符。The MoveNext method increments the index by one, so the first and subsequent characters are accessed in turn. Reset 方法将索引设置为逻辑上位于第一个字符之前的位置。The Reset method sets the index to a position logically before the first character. Current 属性检索索引当前引用的字符。The Current property retrieves the character currently referenced by index. Clone 方法创建 CharEnumerator的副本。The Clone method creates a copy of the CharEnumerator.

备注

一个或多个线程之间 CharEnumerator 的多个独立实例可以访问 String的单个实例。Several independent instances of CharEnumerator across one or more threads can have access to a single instance of String. 实现此类以支持 IEnumerator 接口。This class is implemented to support the IEnumerator interface. 有关使用枚举器的详细信息,请参阅 IEnumerator 主题。For more information regarding the use of an enumerator, see the IEnumerator topic.

属性

Current

获取由此 CharEnumerator 对象枚举的字符串中当前引用的字符。Gets the currently referenced character in the string enumerated by this CharEnumerator object.

方法

Clone()

创建当前 CharEnumerator 对象的副本。Creates a copy of the current CharEnumerator object.

Dispose()

释放 CharEnumerator 类的当前实例所使用的所有资源。Releases all resources used by the current instance of the CharEnumerator class.

Equals(Object)

确定指定的对象是否等于当前对象。Determines whether the specified object is equal to the current object.

(继承自 Object)
GetHashCode()

用作默认哈希函数。Serves as the default hash function.

(继承自 Object)
GetType()

获取当前实例的 TypeGets the Type of the current instance.

(继承自 Object)
MemberwiseClone()

创建当前 Object 的浅表副本。Creates a shallow copy of the current Object.

(继承自 Object)
MoveNext()

递增当前 CharEnumerator 对象的内部索引使其指向枚举的字符串的下一个字符。Increments the internal index of the current CharEnumerator object to the next character of the enumerated string.

Reset()

将索引初始化为逻辑上位于枚举字符串的第一个字符之前的位置。Initializes the index to a position logically before the first character of the enumerated string.

ToString()

返回一个表示当前对象的 string。Returns a string that represents the current object.

(继承自 Object)

显式接口实现

IDisposable.Dispose()

释放 CharEnumerator 类使用的所有资源。Releases all resources used by the CharEnumerator class.

IEnumerator.Current

获取由此 CharEnumerator 对象枚举的字符串中当前引用的字符。Gets the currently referenced character in the string enumerated by this CharEnumerator object. 有关此成员的说明,请参阅 CurrentFor a description of this member, see Current.

适用于

另请参阅