當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


C# CharEnumerator.Reset()用法及代碼示例


CharEnumerator.Reset方法用於將索引初始化為邏輯上在枚舉字符串的第一個字符之前的位置。

用法:

public void Reset ();

以下示例程序旨在說明CharEnumerator.Reset()方法的使用:


示例1:

// C# program to illustrate the 
// use of CharEnumerator.Reset() 
// Method 
using System; 
  
class GFG { 
  
    // Driver code 
    public static void Main() 
    { 
        // Initialize a string object 
        string str = "The Sun rises in the East and sets in the West."; 
  
        // Instantiate a CharEnumerator object 
        CharEnumerator chEnum = str.GetEnumerator(); 
  
        // Printing the string 
        while (chEnum.MoveNext()) 
            Console.Write(chEnum.Current); 
  
        // Reset the CharEnumerator object 
        chEnum.Reset(); 
        Console.WriteLine(); 
  
        // Printing the string again 
        while (chEnum.MoveNext()) 
            Console.Write(chEnum.Current); 
    } 
}
輸出:
The Sun rises in the East and sets in the West.
The Sun rises in the East and sets in the West.

示例2:

// C# program to illustrate the 
// use of CharEnumerator.Reset() 
// Method 
using System; 
  
class GFG { 
  
    // Driver code 
    public static void Main() 
    { 
        // Initialize a string object 
        string str = "0 1 2 3 4 5 6 7 8 9"; 
  
        // Instantiate a CharEnumerator object 
        CharEnumerator chEnum = str.GetEnumerator(); 
  
        // Printing the string 
        while (chEnum.MoveNext()) 
            Console.Write(chEnum.Current); 
  
        // Reset the CharEnumerator object 
        chEnum.Reset(); 
        Console.WriteLine(); 
  
        // Printing the string again 
        while (chEnum.MoveNext()) 
            Console.Write(chEnum.Current); 
    } 
}
輸出:
0 1 2 3 4 5 6 7 8 9
0 1 2 3 4 5 6 7 8 9

參考:



相關用法


注:本文由純淨天空篩選整理自rupesh_rao大神的英文原創作品 C# | CharEnumerator.Reset() Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。