我有這種方法,給我IndexOutOfRangeException,你們可以幫我理解爲什麼嗎?顛倒字符串c#
public string FlipString(string inTxt)
{
StringBuilder outTxt = new StringBuilder();
for (int i = inTxt.Length; i > 0; i--)
{
char ch = inTxt[i];
outTxt.Append(ch);
}
Console.WriteLine(outTxt.ToString());
return outTxt.ToString();
}
的方法已被寫入這樣(沒有例外)
字符串索引都是*從零開始*。 – Jon
有一個linq方法可以做相反的操作:http://tipsandtricks.runicsoft.com/CSharp/StringReverse.html –
下面是使用'Array.Reverse'的方法:http://stackoverflow.com/questions/228038/best-way-to-reverse-a-string –