System.CharEnumerator.MoveNext 方法

方法描述

递增当前 CharEnumerator 对象的内部索引使其指向枚举的字符串的下一个字符。

语法定义(C# System.CharEnumerator.MoveNext 方法 的用法)

public bool MoveNext()

参数/返回值

参数值/返回值 参数类型/返回类型 参数描述/返回描述
返回值 System.Boolean 如果索引递增成功并且在枚举字符串内,则为 true;否则为 false。

提示和注释

CharEnumerator 类维护枚举字符串的内部索引,MoveNext 方法使该索引递增 1。 调用 GetEnumerator 或 Reset 后,请调用 MoveNext 以将当前字符位置递增到枚举字符串中的第一个字符。 检查返回值是否为 true 以确定当前字符位置是否有效。

如果索引已超出枚举字符串中的最后一个字符,则该索引不会被更改并且返回 false。

请注意,如果枚举字符串为空 (""),则 CharEnumerator 的状态始终无效。 这是因为 CharEnumerator 的内部索引最初是在枚举字符串的第一个字符之前,因此无效。 MoveNext 逻辑上将索引设置在枚举字符串的最后一个(不存在的)字符之后,这同样无效。

System.CharEnumerator.MoveNext 方法例子

但请注意,通过使用 foreach(在 C# 中)或 For Each(在 Visual Basic 中)可以在某种程度上更直观地执行相同的操作,如下面的示例所示。

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

异常

异常 异常描述

命名空间

namespace: System

程序集: mscorlib(在 mscorlib.dll 中)

版本信息

.NET Framework 受以下版本支持:4、3.5、3.0、2.0、1.1、1.0 .NET Framework Client Profile 受以下版本支持:4、3.5 SP1

适用平台

Windows 7, Windows Vista SP1 或更高版本, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008(不支持服务器核心), Windows Server 2008 R2(支持 SP1 或更高版本的服务器核心), Windows Server 2003 SP2 .NET Framework 并不是对每个平台的所有版本都提供支持。有关支持的版本的列表,请参见.NET Framework 系统要求。