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. 它會藉CharEnumeratorString.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#程式設計語言的語句, 可逐一查看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.

沒有的CharEnumerator公用函數。There 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參考字串中的字元維護內部索引。CharEnumeratorA 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. 如果您嘗試在索引無效時存取字元, 就會擲回例外狀況 (exception)。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 的淺層複本 (Shallow Copy)。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.

適用於

另請參閱