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. 它通过CharEnumerator MoveNext调用方法来实例化对象, 并通过调用方法从一个字符移动到下一个字符, 并通过检索Current属性的值来显示当前字符。 String.GetEnumeratorIt 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

注解

提供对被引用String对象中的字符的只读访问。 CharEnumeratorA CharEnumerator provides read-only access to the characters in a referenced String object. 例如, foreach Microsoft Visual Basic 和C#编程语言的语句 (可循环访问CharEnumerator集合的元素) 将从String对象检索, 以便循环访问字符在该对象中。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.

重要

类枚举单个16位Char实例。 CharEnumeratorThe 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.

没有公共构造函数CharEnumeratorThere is no public constructor for CharEnumerator. 相反, 调用String对象的GetEnumerator方法以获取CharEnumerator初始化为引用该字符串的。Instead, 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. 方法创建的副本CharEnumeratorCloneThe 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()

返回表示当前对象的字符串。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.

适用于

另请参阅