我如何跳过foreach循环中的空格?

3 c# string foreach

C#newbie here.

我使用foreach语句循环遍历一串字符,并根据它们在字母表中的位置将数值写入int.

这工作正常但是当我尝试跳过字符串中的空格时会抛出此错误:

无法分配给"c",因为它是foreach迭代变量.

这是我的代码:

foreach (char c in encodedText)
{
    if (c = " ");
    {
        continue;
    }

    int index = char.ToUpper(c) - 64;
    Console.WriteLine(index);
}
Run Code Online (Sandbox Code Playgroud)

Dan*_*ite 9

尝试 if (c == ' ')

  • "手段string.
  • '手段char.
  • = 意味着分配.
  • == 意味着平等.