当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


C# CharEnumerator.MoveNext()用法及代码示例


CharEnumerator.MoveNext()方法用于将当前CharEnumerator对象的内部索引递增到枚举字符串的下一个字符。

用法:

public bool MoveNext();

返回值:如果索引成功递增,则此方法返回布尔值true,否则返回false。


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

示例1:

// C# program to illustrate the 
// use of CharEnumerator.MoveNext() 
// Method 
using System; 
  
class GFG { 
  
    // Driver code 
    public static void Main() 
    { 
        // Initialize a string object 
        string str = "A Computer Science Portal for Geeks!"; 
  
        // Instantiate a CharEnumerator object 
        CharEnumerator chEnum = str.GetEnumerator(); 
  
        // Printing the string 
        while (chEnum.MoveNext())  
        { 
            Console.Write(chEnum.Current); 
        } 
    } 
}
输出:
A Computer Science Portal for Geeks!

示例2:

// C# program to illustrate the 
// use of CharEnumerator.MoveNext() 
// Method 
using System; 
  
class GFG { 
  
    // Driver code 
    public static void Main() 
    { 
        // Initialize a string object 
        string str = "Best Data Structure and Algorithms Tutorials!"; 
  
        // Instantiate a CharEnumerator object 
        CharEnumerator chEnum = str.GetEnumerator(); 
  
        // Printing the string 
        while (chEnum.MoveNext())  
        { 
            if (chEnum.Current == ' ')  
            { 
                Console.WriteLine(); 
                continue; 
            } 
            Console.Write(chEnum.Current); 
        } 
    } 
}
输出:
Best
Data
Structure
and
Algorithms
Tutorials!

参考:



相关用法


注:本文由纯净天空筛选整理自rupesh_rao大神的英文原创作品 C# | CharEnumerator.MoveNext() Method。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。