我在这行代码中得到索引越界错误:
---------------------------vvvvv-------------------------------------------
>>> char character = Convert.ToChar(temp[1].Substring(0, 1).ToLower()); <<<
---------------------------^^^^^-------------------------------------------
(整个代码块)
List<string> entries = new List<string>();
List<string> pre_entries = new List<string>((string[])Regex.Split(data.Substring(3, data.Length - 3), ",,"));
foreach (string message in pre_entries)
{
string[] temp = message.Split(new string[] { "~~||~~" }, StringSplitOptions.RemoveEmptyEntries);
if (temp.Length == 2)
{
char character = Convert.ToChar(temp[1].Substring(0, 1).ToLower());
if (character == 'a' || character == 'b' || character == 'c' || character == 'd' || character == 'e' ||
character == 'f' || character == 'g' || character == 'h' || character == 'i' || character == 'j' ||
character == 'k' || character == 'l' || character == 'm' || character == 'n' || character == 'o' ||
character == 'p' || character == 'q' || character == 'r' || character == 's' || character == 't' ||
character == 'u' || character == 'v' || character == 'w' || character == 'x' || character == 'y' ||
character == 'z')
{
entries.Add(ReplaceFirst(temp[1].Substring(13), ",", ": "));
}
else if (temp.Length > 2)
{
290: char character = Convert.ToChar(temp[1].Substring(0, 1).ToLower());
if (character == 'a' || character == 'b' || character == 'c' || character == 'd' || character == 'e' ||
character == 'f' || character == 'g' || character == 'h' || character == 'i' || character == 'j' ||
character == 'k' || character == 'l' || character == 'm' || character == 'n' || character == 'o' ||
character == 'p' || character == 'q' || character == 'r' || character == 's' || character == 't' ||
character == 'u' || character == 'v' || character == 'w' || character == 'x' || character == 'y' ||
character == 'z')
{
for (int i = 0; i < temp.Length / 2; i++)
{
entries.Add(ReplaceFirst(temp[i + 1], ",", ": "));
}
}
else
{
for (int i = 0; i < temp.Length / 2; i++)
{
entries.Add(ReplaceFirst(temp[i + 1].Substring(13), ",", ": "));
}
}
}
else { }
}
假设我们进入此foreach循环的数据格式为List<string>
,列表的数据类似于“554655 ~~~ || ~~~ 123454,2 User,blah 554655 ~~~ || ~~~ 123454,2 User2,stuff 545675 ~~~ || ~~~~ 567875,User3“。数据1,54134567851~~~||~~~
的第一部分无关紧要,因为此示例中分隔符前的字符数是错误的。我不确定什么类型的消息设置了捕获但是每当我在应用程序(聊天应用程序)中出现错误时它都会显示此错误。
错误:异常
来源:SimpleMan
消息:索引超出了数组的范围。
StackTrace:位于X:\ Projects \ Visual Studio Projects \ c#\ SimpleMan \ SimpleMan \ Program.cs:第290行的SimpleMan.Server.Message()中
HResult:-2146233080
数据:System.Collections.ListDictionaryInternal
示例:
如果我在索引0的pre_entries列表中有一个字符串:“554655 ~~~ || ~~~ 123454,2 User,blah 554655 ~~~ || ~~~ 123454,2 User2,stuff 545675 ~~ 〜|| ~~~~ 567875,User3“
临时字符串数组将从第一个开始有4个条目,
(指数0):“554655
(索引1):“~~~ || ~~~ 123454,2 User,blah 554655”
(索引2):“~~~ || ~~~ 123454,2 User2,stuff 545675”
(索引3):“~~~ || ~~~~ 567875,User3”
有时其中一个字符串在temp中看起来像“12332354 ~~~ || ~~~ 546546,User,blah”这就是为什么我有if / then语句寻找这种可能的字符不匹配的原因情况。
答案 0 :(得分:1)
看起来temp
没有2个条目。在尝试检索之前确保它确实存在。
注意:这可能不言而喻,但数组的索引为0。这意味着,如果您想要第一个条目,请执行[0]
而不是[1]
。