我的应用程序中不断出现此错误 这段代码的工作原理是多么简单的一小段
String lang = "Language.en-US".ToString();
appPath.Text = lang.Substring(2, lang.Length);//
错误
System.ArgumentOutOfRangeException:
Index and length must refer to a location within the string
每次更改起始索引时都会出现错误,但如果我将其设置为 0,它会起作用,但我无法将起始索引更改为大于零的数字,有什么我可以下载的更新来解决这个问题我确定我的系统是错误的,我使用的是 .Net 4.0。
最佳答案
你应该把它改成
appPath.Text = lang.Substring(2, lang.Length - 2);
基本按照Substring方法文档
public string Substring(
int startIndex,
int length
)
startIndex + length
应小于字符串长度。
如果你总是想得到从第二个位置到最后的字符串,你应该使用重载版本的 Substring 也就是
public string Substring(
int startIndex
)
关于c# - 我在起始索引上有一个子串错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8651687/