C# 长度不能为负(例外)

C# 长度不能为负(例外),c#,C#,我一直在做一个关于codeeval的练习: 这是我的密码 using System; using System.Text; using System.IO; using System.Collections.Generic; class Program { static void Main(string[] args) { using (StreamReader reader = File.OpenText(args[0])) while (!

我一直在做一个关于codeeval的练习:

这是我的密码

using System;
using System.Text;
using System.IO;
using System.Collections.Generic;

class Program
{
    static void Main(string[] args)
    {
        using (StreamReader reader = File.OpenText(args[0]))
        while (!reader.EndOfStream)
        {
            StringBuilder result = new StringBuilder();
            string b = "";
            string c = "";
            string[] b2;
            string[] c2;
            int x = 0;
            string line = reader.ReadLine();
            if (null == line)
                continue;
            // do something with line
            else{
            x = line.IndexOf(";");
            b = line.Substring(x +1);
            b2 = b.Split(',');
            c = line.Substring(0, (line.Length - ((b.Length) + 1)));
            c2 = c.Split(',');
            foreach (string u in b2) {
                    foreach (string i in c2)
                    {
                        if (int.Parse(u) == int.Parse(i))
                        {
                            result.Append(u + ",");
                        }
                    }
                }
            Console.WriteLine(result.ToString().Substring(0, result.Length - 1));
            result.Clear();
            Array.Clear(b2, 0 , b2.Length);
            Array.Clear(c2, 0 , c2.Length);
            }
        }
    }
}
当我在我的VS上运行时,它在1个字符串上工作。然而,当我在codeeval上运行它时,它会运行一两行代码,然后给我一个异常。我不知道为什么长度可以是负数

未处理的异常:System.ArgumentOutOfRangeException:不能为 消极的参数名称:System.String.Substring处的长度(Int32 startIndex,Int32长度)[0x00000]英寸:0英寸 0中的Program.Main(System.String[]args)[0x00000] [错误]致命的未处理异常:System.ArgumentOutOfRangeException: 不能是负数。参数名称:长度为 System.String.Substring(Int32 startIndex,Int32长度)[0x00000]英寸 :0位于Program.Main(System.String[]args) [0x00000]英寸:0

输入是

68,69,70,71,72,73,74,75,76,77,78,79,80,81,82;78,79,80,81,82,83,84
87,88,89,90,91,92,93;64,65,66,67,68
87,88,89,90,91,92;81,82,83,84,85,86,87
82,83,84,85,86;68,69,70,71,72,73,74,75,76,77,78,79,80
78,79,80,81,82,83,84,85,86,87,88,89,90,91;67,68,69,70,71,72

如果
如果(int.Parse(u)==int.Parse(i))
对于所有迭代都为false,则
结果将为空。因此,您将获得
result=“
,并在提取子字符串
result时向上。Length-1
将指向负索引。这是无效的

.Substring(0, result.Length - 1)// points to a negative index which is invalid.
通过在处理结果值之前检查结果值,可以避免此类错误。即

if(result.ToString()!="")
{
  //Process your code
}
  • 您的输入不包含
    “;”
  • 所以
    x=-1
    b=line
    (所以
    line.Length=b.Length
  • line.Length-((b.Length)+1)=-1
因此,您调用长度为
-1
子字符串

[更新]哦,刚刚意识到有
在您的输入中。无论如何,如果不验证输入,我的示例仍然会导致相同的异常。所以我不会删除这个答案,因为它仍然很有意义。

考虑一下这一行:

c = line.Substring(0, (line.Length - ((b.Length) + 1)));
如果
不包含
然后:

  • x=line.IndexOf(“;”)
    x
    设置为-1
  • b=line.Substring(x+1)将导致
    b==line
  • line.Length-((b.Length)+1)
    将导致一个负数

  • 我想还有其他故障模式。

    编辑链接:要么
    (line.Length-((b.Length)+1))
    返回负数。你需要找出为什么
    b
    line
    result.Length-1
    返回一个负数(即
    result
    是一个空字符串)。使用debug并查看发生了什么。
    c = line.Substring(0, (line.Length - ((b.Length) + 1)));