CharEnumerator CharEnumerator CharEnumerator CharEnumerator Class

定义

支持循环访问 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)
继承
CharEnumeratorCharEnumeratorCharEnumeratorCharEnumerator
属性
实现

示例

下面的示例使用CharEnumerator类来枚举字符串中的单个字符。The following example uses the CharEnumerator class to enumerate the individual characters in a string. 它实例化CharEnumerator对象通过调用String.GetEnumerator方法,从一个字符移到下一个通过调用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. 例如,foreach语句的 Microsoft Visual Basic 和 C# 编程语言,它循环访问集合中的元素,检索CharEnumeratorString对象以循环访问中的字符对象。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.

没有公共构造函数为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方法递增索引 1,因此依次访问第一个和后续字符。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方法创建一份CharEnumeratorThe Clone method creates a copy of the CharEnumerator.

备注

多个独立实例的CharEnumerator跨一个或多个线程可以有权访问的单个实例StringSeveral 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 Current Current Current

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

方法

Clone() Clone() Clone() Clone()

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

Dispose() Dispose() Dispose() Dispose()

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

Equals(Object) Equals(Object) Equals(Object) Equals(Object)

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

(Inherited from Object)
GetHashCode() GetHashCode() GetHashCode() GetHashCode()

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

(Inherited from Object)
GetType() GetType() GetType() GetType()

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

(Inherited from Object)
MemberwiseClone() MemberwiseClone() MemberwiseClone() MemberwiseClone()

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

(Inherited from Object)
MoveNext() MoveNext() MoveNext() MoveNext()

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

Reset() Reset() Reset() Reset()

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

ToString() ToString() ToString() ToString()

返回表示当前对象的字符串。Returns a string that represents the current object.

(Inherited from Object)

显式界面实现

IDisposable.Dispose() IDisposable.Dispose() IDisposable.Dispose() IDisposable.Dispose()

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

IEnumerator.Current IEnumerator.Current IEnumerator.Current IEnumerator.Current

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

适用于

另请参阅