C# “索引和长度必须引用字符串中的某个位置”错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14881617/
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-10 13:19:35  来源:igfitidea点击:

"Index and length must refer to a location within the string" error

c#stringindexing

提问by Edwin Torres

I am trying to extract the first 200 words of a string and sometimes I get the following error:

我试图提取字符串的前 200 个单词,有时会出现以下错误:

"Index and length must refer to a location within the string. Parameter name: length"

The code is:

代码是:

int i = GetIndex(fullarticle, 200);
string result = fullarticle.Substring(0, i);

How do I fix this?

我该如何解决?

采纳答案by Austin Salonen

It appears safe to assume the error is coming from string.Substring. Given that you get this error when startIndex + length > given.Lengthor startIndex < 0or length < 0, GetIndexis either returning a value greater than fullarticle.Lengthor a negative number. The error exists in GetIndexso if you wish to carry on with the code you have, you should post the code of GetIndexto get the best answer.

假设错误来自string.Substring似乎是安全的。鉴于您在startIndex + length > given.Length或时收到此错误startIndex < 0length < 0GetIndex返回大于fullarticle.Length或负数的值。错误存在于GetIndex因此如果您希望继续使用您拥有的代码,您应该发布代码GetIndex以获得最佳答案。

If you're up for something different, you could try this:

如果你想要不同的东西,你可以试试这个:

static string GetShortIntroduction(string phrase, int words)
{
    // simple word count assuming spaces represent word boundaries
    return string.Join(" ", phrase.Split().Take(words));
}

回答by dead_ant

Looks like i is larger than the whole fullarticle length. Check your GetIndex function.

看起来 i 比整个 fullarticle 长度大。检查您的 GetIndex 函数。

回答by Russ Cam

This is probably happening because the string has less than 200 words in it and likely coming from GetIndexreturn a value for igreater than the number of characters in fullarticle. As an example of the error

这可能是因为字符串中的单词少于 200 个,并且可能来自GetIndex返回的值i大于fullarticle. 作为错误的一个例子

"s".Substring(0,2)

throws

投掷

ArgumentOutOfRangeException: Index and length must refer to a location within the string. Parameter name: length

ArgumentOutOfRangeException:索引和长度必须引用字符串中的某个位置。参数名称:长度

If your intent is to get up to the first 200 words in a string, you would need to check

如果您的目的是获取字符串中的前 200 个单词,则需要检查

  1. The string is not null
  2. The number of words in the string; if it's less than 200 words, that should be your maximum index, otherwise use 200.
  3. substring based on 2.
  1. 字符串不为空
  2. 字符串中的单词数;如果少于 200 个字,那应该是你的最大索引,否则使用 200。
  3. 基于 2 的子串

回答by Matas Vaitkevicius

It goes out of range as your string is shorter than 200 characters

它超出范围,因为您的字符串少于 200 个字符

To remedy you could use Math.Minit will pick the lower value between string length and 200.

为了补救,您可以使用Math.Min它会选择字符串长度和 200 之间的较低值。

fullarticle.Substring(0, Math.Min(fullarticle.Length, 200));

Hope this saves you some time.

希望这可以为您节省一些时间。