在以下代码中:
static void Main(string[] args)
{
string MultiLineString = @"This is a
random sentence";
int index=0;
string test = "";
Console.WriteLine(MultiLineString[9].ToString()); //it should print 'r' but it prints a white space
for (int i = 0; i < MultiLineString.Length; i++)
{
if (MultiLineString[i] == 'r')
index = i;
}
Console.WriteLine(index); // 11 is the index of 'r' in "random"
foreach (char ch in MultiLineString)
if (ch == ' ')
test += "_";
else
test += ch;
Console.WriteLine(test);
// the output is:
// This_is_a
//random_sentece
}
我很难理解 9-10 索引中发生了什么。起初我以为这是一个空格,当我跳过一行时以某种方式创建了它,但后来它没有包含在测试字符串中。
提前致谢。