0

我正在通过备忘录字段循环字符,但我不知道如何停止循环。是否有像 EOF 字符这样的字段结束值?有没有更好的方法来确定备忘录字段的结尾?

4

2 回答 2

1

如果您使用foreach,则无需担心...

        string memo = "test";
        foreach (var x in memo.ToCharArray())
        {
            // you code here
        }
于 2011-04-15T19:36:01.100 回答
1

我不确定您所说的“备忘录字段”是什么意思。你的意思是一个字符串?如果是这样,那么您可以访问该Length属性:

string memo = "hello, world";
for (int i = 0; i < memo.Length; ++i)
{
    char c = memo[i];
    // do what you want with the character.
}

或者您可以使用foreach之前建议的方法:

foreach (var c in memo)
{
    // do what you want with the character
}
于 2011-04-15T20:15:41.823 回答