我真的很困惑为什么我会得到一个例外。这是我放在一起演示的 SSCCE:
static void Main(string[] args)
{
string tmp =
"Child of: View Available Networks (197314), Title: N/A (66244)";
Console.WriteLine(tmp);
int one = tmp.LastIndexOf('('), two = tmp.LastIndexOf(')');
//my own error checking
Console.WriteLine(tmp.Length);//returns 63
Console.WriteLine(one < 0);//returns false
Console.WriteLine(two > tmp.Length);//returns false
Console.WriteLine(one);//returns 56
Console.WriteLine(two);//returns 62
/*
* error occurs here.
* ArgumentOutOfRangeException Index and length must refer to
* a location within the string.
* Parameter name: length
*/
string intptr = tmp.Substring(one, two);
Console.WriteLine(intptr);
}
我看不出我做错了什么(虽然来自 Java 背景可能是微不足道的),希望其他人可以。