我在起始索引上有一个子字符串错误

ONY*_*NYX 2 c# asp.net

我在我的应用程序小片段中不断收到此错误,该代码假设工作有多么简单

String lang = "Language.en-US".ToString();
appPath.Text = lang.Substring(2, lang.Length);//
Run Code Online (Sandbox Code Playgroud)

错误

System.ArgumentOutOfRangeException: 
Index and length must refer to a location within the string
Run Code Online (Sandbox Code Playgroud)

我每次更改起始索引时都会收到错误但如果我将其设置为0则可以正常工作,但我无法将起始索引更改为高于零的数字是否有任何我可以下载的内容,如更新以解决此问题我确定它我的系统错误地使用.Net 4.0.

par*_*mar 5

你应该把它改成

appPath.Text = lang.Substring(2, lang.Length - 2);
Run Code Online (Sandbox Code Playgroud)

基本上是根据Substring方法的文档

public string Substring(
    int startIndex,
    int length
)
Run Code Online (Sandbox Code Playgroud)

startIndex + length 应该小于字符串长度.

如果你总是想从第二位置串到了最后,你应该使用重载版本的子字符串

public string Substring(
    int startIndex
)
Run Code Online (Sandbox Code Playgroud)