我有一个 for 循环,将数组中的项目添加到 listView。
(它会抓取网页上的项目,删除字符串中 ' 之后的任何内容,然后将其添加到 listView 中)
我得到的错误是:IndexOutOfRangeException was unhandled- Index was outside the bounds of the array
这是我正在使用的代码:
string[] aa = getBetweenAll(vid, "<yt:statistics favoriteCount='0' viewCount='", "'/><yt:rating numDislikes='");
for (int i = 0; i < listView1.Items.Count; i++)
{
string input = aa[i];
int index = input.IndexOf("'");
if (index > 0)
input = input.Substring(0, index);
listView1.Items[i].SubItems.Add(input);
}
错误发生在这一行:string input = aa[i];
我做错什么了吗?我该如何解决这个问题,让它停止发生?谢谢!
如果您想知道getBetweenAll方法的代码是:
private string[] getBetweenAll(string strSource, string strStart, string strEnd)
{
List<string> Matches = new List<string>();
for (int pos = strSource.IndexOf(strStart, 0),
end = pos >= 0 ? strSource.IndexOf(strEnd, pos) : -1;
pos >= 0 && end >= 0;
pos = strSource.IndexOf(strStart, end),
end = pos >= 0 ? strSource.IndexOf(strEnd, pos) : -1)
{
Matches.Add(strSource.Substring(pos + strStart.Length, end - (pos + strStart.Length)));
}
return Matches.ToArray();
}