C# System.ArgumentOutOfRangeException:startIndex 不能大于字符串的长度

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9813039/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 10:43:16  来源:igfitidea点击:

System.ArgumentOutOfRangeException: startIndex cannot be larger than length of string

c#asp.net-mvc

提问by

I have this code. I am trying to retrieve just the text "first program". Considering that i know the index say 25 and total length of string is 35.

我有这个代码。我试图只检索文本“第一个程序”。考虑到我知道索引说 25 并且字符串的总长度是 35。

string text="Hello world ! This is my first program";

Response.Write(text.SubString(25,35));

But i get the error during runtime "System.ArgumentOutOfRangeException: startIndex cannot be larger than length of string"

但是我在运行时收到错误“System.ArgumentOutOfRangeException:startIndex 不能大于字符串的长度”

采纳答案by pjumble

The parameters for String.Substringare:

参数为String.Substring

public string Substring(
    int startIndex,
    int length
)

You're trying to take 35 characters afterthe 26th character (startIndex is zero-based), which is out of range.

您试图第 26 个字符(startIndex 从零开始)之后取 35 个字符,这超出了范围。

If you just want to get from the 25th character to the end of the string use text.SubString(24)

如果您只想从第 25 个字符到字符串末尾,请使用 text.SubString(24)

回答by BrokenGlass

Second argument to string.Substring()is the length, not the end-offset:

第二个参数string.Substring()length,而不是 end-offset :

Response.Write(text.Substring(25,10));

回答by porges

The second parameter to Substringis how long you want the substring to be, not the end point of the substring. 25 + 35is outside the range of the original string, so it throws an exception.

第二个参数 toSubstring是您希望子字符串的长度,而不是子字符串的终点。25 + 35超出原始字符串的范围,因此会引发异常。

回答by DotNetUser

Second argument for SubString is the number of characters in the substring.

SubString 的第二个参数是子字符串中的字符数。

Simpler way to do it.

更简单的方法来做到这一点。

    int startIndex=25; // find out startIndex
    int endIndex=35;   // find out endIndex, in this case it is text.Length;
    int length= endIndex - startIndex; // always subtract startIndex from the position wherever you want your substring to end i.e. endIndex
    // call substring
    Response.Write(text.Substring(startIndex,length));     

you can do some operation or call a function to get start/end index values. With this approach you are less likely to get into any trouble related to indexes.

您可以执行一些操作或调用函数来获取开始/结束索引值。使用这种方法,您不太可能遇到与索引相关的任何问题。

回答by Duk

During this time you can use

在此期间您可以使用

(LINQ ElementAt)and (ElementAtOrDefault)method. However the ElementAtextension method would throw the System.ArguementOutOfRangeExceptionwhen the specified index is a negative value or not less than the size of the sequence .

(LINQ ElementAt)(ElementAtOrDefault)方法。但是,当指定的索引为负值或不小于序列的大小时,ElementAt扩展方法将抛出System.ArguementOutOfRangeException