C# System.ArgumentOutOfRangeException:索引和长度必须引用字符串中的位置
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17578932/
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
System.ArgumentOutOfRangeException: Index and length must refer to a location within the string
提问by user2569756
When I am executing my code on visual studio it is working good, but when i execute my exe file, it is throwing an exception.
当我在 Visual Studio 上执行我的代码时,它运行良好,但是当我执行我的 exe 文件时,它会引发异常。
System.ArgumentOutOfRangeException: Index and length must refer to a location within the string.
Parameter name: length
at System.String.InternalSubStringWithChecks(Int32 startIndex, Int32 length, Boolean fAlwaysCopy)
at System.String.Substring(Int32 startIndex, Int32 length)
at QuoteExtractor.frmQuoteExtractor.cmdProcess_Click(Object sender, EventArgs e)
My code
我的代码
private void cmdProcess_Click(object sender, EventArgs e)
{
try
{
foreach (string line in rtfMain.Lines)
{
try
{
if (line.Trim() != "")
{
if ((line != "") && (line != " ") && (line.Substring(0, 3) != "***") && (line.Substring(0, 5) != "CUSIP"))
{
Quote q = new Quote();
q.Parse(line);
DisplayQuote(q);
QuoteList.Add(q);
}
}
}
Input:
输入:
*** 9:30 AM ***
CUSIP BOND NAME SIZE CURR FACE TALK COVER PX
004421GU1 ACE 2004-HE2 M2 10.000MM 1.103MM dnt, 90 rsrv
61744CTZ9 MSHEL 2005-3 M3 4.720MM 4.720MM dnt
回答by jason
Read the error message.1One of
阅读错误消息。1其中之一
line.Substring(0, 3) != "***"
or
或者
line.Substring(0, 5) != "CUSIP"
is bad because the length of lineis less than three or five, respectively. Also, use String.StartsWithfor this purpose.
不好是因为 的长度line分别小于三或五。此外,String.StartsWith用于此目的。
1: The error message is practically screaming at you:
1:错误信息实际上是在向你尖叫:
System.ArgumentOutOfRangeException:Indexandlengthmust refer to a location within the string. Parameter name:lengthatSystem.String.InternalSubStringWithChecks(Int32 startIndex, Int32 length, Boolean fAlwaysCopy)atSystem.String.Substring(Int32 startIndex, Int32 length)
System.ArgumentOutOfRangeException:Index并且length必须引用字符串中的某个位置。参数名称:lengthatSystem.String.InternalSubStringWithChecks(Int32 startIndex, Int32 length, Boolean fAlwaysCopy)atSystem.String.Substring(Int32 startIndex, Int32 length)
It could not be any more clear that there is a problem with the lengthargument that you're passing to String.Substring. From the documentation:
length您传递给的参数有问题再清楚不过了String.Substring。从文档:
Exceptions
ArgumentOutOfRangeException
startIndexpluslengthindicates a position not within this instance.-or-
startIndexorlengthis less than zero.
例外
ArgumentOutOfRangeException
startIndex加号length表示不在此实例内的位置。-或者-
startIndex或length小于零。
Your startIndexvalues aren't less than zero. Your lengthvalues aren't less than zero. That rules out the second possibility. The first possibility is that startIndexplus lengthindicates a position not within the string. Your startIndexvalues are zero, so startIndexplus lengthequals length. So the error message is telling you that lengthrefers to a position not within the string. Thus, lengthexceedsthe length of your string. It can notbe anymore clear.
您的startIndex值不小于零。您的length值不小于零。这就排除了第二种可能性。第一种可能性是startIndex加号length表示不在字符串内的位置。您的startIndex值为零,因此startIndexpluslength等于length。因此,错误消息告诉您,它length指的是不在字符串内的位置。因此,length超过了字符串的长度。它不能是再清楚。
回答by nvoigt
If you don't know if your string actually contains enough characters for a substring, you should use StartsWithfor your comparison.
如果您不知道您的字符串实际上是否包含足够的子字符串字符,您应该使用StartsWith进行比较。
回答by HuorSwords
The exception are saying you that on
例外是说你在
line.Substring(0, 3) != "***"
or on
或在
line.Substring(0, 5) != "CUSIP"
you are referring a string location that does not exist, probably because the linevalue length is less than expected.
您指的是一个不存在的字符串位置,可能是因为line值长度小于预期。
An alternate piece of code could be...
另一段代码可能是......
private void cmdProcess_Click(object sender, EventArgs e)
{
foreach (string line in rtfMain.Lines)
{
if (!string.IsNullOrEmpty(line.Trim())
&& !line.StartsWith("***")
&& !line.StartsWith("CUSIP"))
{
Quote q = new Quote();
q.Parse(line);
DisplayQuote(q);
QuoteList.Add(q);
}
}
}
回答by Rado
I'm having the same problem with the attached code - VS doesn't catch the error if the "line" string is less than 57 chars. It doesn't react to the test code either - that code is from a test project I've made in another concurrent instance of VS where the debugger doescatch the error. If I put the code in the main project in a try-catch it does get the error though.
我对附加的代码有同样的问题 - 如果“行”字符串少于 57 个字符,VS 不会捕获错误。它也不会对测试代码做出反应——该代码来自我在 VS 的另一个并发实例中制作的测试项目,调试器确实在其中捕获了错误。如果我将代码放在主项目中的 try-catch 中,它确实会收到错误。
I've seen it once before and suspect it's either a bug in Visual Studio (2008 Pro in this case - don't know if it affects later versions, just upgraded to VS 2013 and haven't seen it there yet) or caused by a corrupted project file.
我以前见过一次,怀疑它是 Visual Studio 中的错误(在这种情况下为 2008 Pro - 不知道它是否会影响更高版本,刚刚升级到 VS 2013 并且还没有在那里看到它)或由损坏的项目文件。
while (rf.Peek() >= 0)
{
line = rf.ReadLine().Trim();
MessageBox.Show(line.Length.ToString());
// test
string x = "abc";
string y = x.Substring(0, 20);
// end test
if (line.Substring(0, 57) == "<td align=\"right\" nowrap=\"nowrap\"><span class=\"currency\">")
{
break;
}
// some more code
}

