检查行是否有参数else跳过该行

时间:2014-08-31 09:35:59

标签: c#-4.0 visual-studio-2012

我有这个:

if (line.Contains("nosyn_name_last_exact:(qxq"))
{
    var temp = line.Split(FirstSeparator, StringSplitOptions.RemoveEmptyEntries)[1];
    if (temp.Length > 1)
    {
         var result2 = temp.Split(SecondSeparator, 
                                  StringSplitOptions.RemoveEmptyEntries)[0];
         //result2[1].Split(')')[0].Dump();
         listNames.Add(result2);
    }

    //Console.WriteLine(result2);      
}

这是输出:

Console.WriteLine("Url " + "\t\t\t\tLast Name");
int index = -1;

foreach (var key in times.Keys)
{
    if (key.Contains("Url"))
    {
        index++;
        Console.WriteLine("{0}: {1} {2}", key, times[key].TotalSeconds, "\t\t" + listNames[index]);
    }
    else
    {
        Console.WriteLine("{0}: {1}", key, times[key].TotalSeconds);
    }
}

这就是我使用的行:

23.9.66:80/solr_3.6/wiewaswie_live/select/  qt=edismax_nosyn_a2aperson&q=(johanna huisman)&fq={!tag%3Dalldoctypes}doc_type:1&fq=date_main:[18980000 TO 19773112]&facet.query={!ex%3Dalldoctypes}doc_type:3 AND (b_public:1 OR fk_account:(16209514))&facet.query={!ex%3Dalldoctypes}doc_type:2 AND (b_public:1 OR fk_account:(16209514))&spellcheck=true&spellcheck.count=-3&start=230&sort=name_last asc, score desc&omitHeader=true
2014-08-25 15:36:32,246 DEV avqih5cp053gloqfdtd5iaez    0   Q   .   http://10.123.9.66:80/solr_3.6/wiewaswie_live/select/   qt=edismax_nosyn_a2aperson&q=(Pieter van der Meulen)&fq={!tag%3Dalldoctypes}doc_type:1&fq=(fk_province:1)&facet.query={!ex%3Dalldoctypes}doc_type:3 AND (b_public:1)&facet.query={!ex%3Dalldoctypes}doc_type:2 AND (b_public:1)&spellcheck=true&spellcheck.count=-3&start=20&sort=name_last asc, score desc&omitHeader=true
2014-08-25 15:36:32,324 DEV avqih5cp053gloqfdtd5iaez    89  R   O   http://10.123.9.66:80/solr_3.6/wiewaswie_live/select/   qt=edismax_nosyn_a2aperson&q=(Pieter van der Meulen)&fq={!tag%3Dalldoctypes}doc_type:1&fq=(fk_province:1)&facet.query={!ex%3Dalldoctypes}doc_type:3 AND (b_public:1)&facet.query={!ex%3Dalldoctypes}doc_type:2 AND (b_public:1)&spellcheck=true&spellcheck.count=-3&start=20&sort=name_last asc, score desc&omitHeader=true
2014-08-25 15:36:32,698 DEV 1eltnkbwb1na30pvxp5nsaxi    0   Q   .   http://10.123.9.66:80/solr_3.6/wiewaswie_live/select/   qt=edismax_nosyn_a2aperson&q=(willem breedenoord)&fq={!tag%3Dalldoctypes}doc_type:1&facet.query={!ex%3Dalldoctypes}doc_type:3 AND (b_public:1)&facet.query={!ex%3Dalldoctypes}doc_type:2 AND (b_public:1)&spellcheck=true&spellcheck.count=-3&start=0&sort=name_last asc, score desc&omitHeader=true
2014-08-25 15:36:32,730 DEV 1eltnkbwb1na30pvxp5nsaxi    35  R   O   http://10.123.9.66:80/solr_3.6/wiewaswie_live/select/   qt=edismax_nosyn_a2aperson&q=(willem breedenoord)&fq={!tag%3Dalldoctypes}doc_type:1&facet.query={!ex%3Dalldoctypes}doc_type:3 AND (b_public:1)&facet.query={!ex%3Dalldoctypes}doc_type:2 AND (b_public:1)&spellcheck=true&spellcheck.count=-3&start=0&sort=name_last asc, score desc&omitHeader=true
2014-08-25 15:36:32,792 DEV 0pqbpdeurbtlp312ysf12nu1    2321    R   O   http://10.123.9.66:80/solr_3.6/wiewaswie_live/select/   qt=standard_a2aperson&q=*:*&fq=(nosyn_name_last_exact:(qxqbroekhuijzenqxq))&spellcheck.q=(qxqbroekhuijzenqxq)&fq={!tag%3Dalldoctypes}doc_type:1&fq=date_main:[17000101 TO 18503112]&facet.query={!ex%3Dalldoctypes}doc_type:3 AND (b_public:1)&facet.query={!ex%3Dalldoctypes}doc_type:2 AND (b_public:1)&spellcheck=true&spellcheck.count=-3&start=0&sort=name_last asc, score desc&omitHeader=true

因此,如果一行包含参数:nosyn_name_last_exact:(qxq

然后它必须显示结果,否则擦除线条。但我得到错误:索引超出了数组的范围。通过这段代码:

catch (Exception e)   
{
    Console.WriteLine("An error occured while attempting to access the source file at {0}", sourceFile);
}
哦,如果我这样做:

var source = "2014-08-25 15:36:32,730 DEV 1eltnkbwb1na30pvxp5nsaxi  35  R   O   http://10.123.9.66:80/solr_3.6/wiewaswie_live/select/   qt=edismax_nosyn_a2aperson&q=(willem breedenoord)&fq={!tag%3Dalldoctypes}doc_type:1&facet.query={!ex%3Dalldoctypes}doc_type:3 AND (b_public:1)&facet.query={!ex%3Dalldoctypes}doc_type:2 AND (b_public:1)&spellcheck=true&spellcheck.count=-3&start=0&sort=name_last asc, score desc&omitHeader=true"; 
string[] FirstSeparator = new string[] { "nosyn_name_last_exact:(qxq" };
string[] SecondSeparator = new string[] { "qxq)" };
var temp = source.Split(FirstSeparator, StringSplitOptions.RemoveEmptyEntries)[1];
var result = temp.Split(SecondSeparator, StringSplitOptions.RemoveEmptyEntries)[0];
result.Dump("Result: ");

我收到错误:

索引超出了数组的范围。

但如果我这样做:

var source = "2014-08-25 15:36:32,792 DEV 0pqbpdeurbtlp312ysf12nu1  2321    R   O   http://10.123.9.66:80/solr_3.6/wiewaswie_live/select/   qt=standard_a2aperson&q=*:*&fq=(nosyn_name_last_exact:(qxqbroekhuijzenqxq))&spellcheck.q=(qxqbroekhuijzenqxq)&fq={!tag%3Dalldoctypes}doc_type:1&fq=date_main:[17000101 TO 18503112]&facet.query={!ex%3Dalldoctypes}doc_type:3 AND (b_public:1)&facet.query={!ex%3Dalldoctypes}doc_type:2 AND (b_public:1)&spellcheck=true&spellcheck.count=-3&start=0&sort=name_last asc, score desc&omitHeader=true"; 
string[] FirstSeparator = new string[] { "nosyn_name_last_exact:(qxq" };
string[] SecondSeparator = new string[] { "qxq)" };
var temp = source.Split(FirstSeparator, StringSplitOptions.RemoveEmptyEntries)[1];
var result = temp.Split(SecondSeparator, StringSplitOptions.RemoveEmptyEntries)[0];
result.Dump("Result: ");

然后结果将是: 结果:

broekhuijzen

但是我在文本文件中有更多行,所以如果没有找到,则跳过。

哦,我现在就这样:

var source = "2014-08-25 15:36:32,792 DEV 0pqbpdeurbtlp312ysf12nu1  2321    R   O   http://10.123.9.66:80/solr_3.6/wiewaswie_live/select/   qt=standard_a2aperson&q=*:*&fq=(nosyn_name_last_exact:(qxqbroekhuijzenqxq))&spellcheck.q=(qxqbroekhuijzenqxq)&fq={!tag%3Dalldoctypes}doc_type:1&fq=date_main:[17000101 TO 18503112]&facet.query={!ex%3Dalldoctypes}doc_type:3 AND (b_public:1)&facet.query={!ex%3Dalldoctypes}doc_type:2 AND (b_public:1)&spellcheck=true&spellcheck.count=-3&start=0&sort=name_last asc, score desc&omitHeader=true"; 
string[] FirstSeparator = new string[] { "nosyn_name_last_exact:(qxq" };
string[] SecondSeparator = new string[] { "qxq)" };
var temp = source.Split(FirstSeparator, StringSplitOptions.RemoveEmptyEntries);
if(temp.Length > 1)
{
temp[1].Split(')')[0].Dump();
//var result = temp.Split(SecondSeparator, StringSplitOptions.RemoveEmptyEntries)[0];
//result.Dump("Result: ");
}

结果是:broekhuijzenqxq。但它必须是:broekhuijzen,所以没有qxq

我只是不明白为什么这里出错:

foreach (var key in times.Keys)
            {
                if (key.Contains("Url"))
                {
                    index++;
                    Console.WriteLine("{0}:{1} {2}",key, times[key].TotalSeconds, "\t\t" + listNames[index]);
                }
                else
                {
                    Console.WriteLine("{0}:{1}",key, times[key].TotalSeconds);
                }
            }

它计数9 - 所以这是正确的,但我仍然得到这一行的错误:listNames [index]); -

未处理的类型' System.ArgumentOutOfRangeException'发生在mscorlib.dll

其他信息:指数超出范围。必须是非负数且小于集合的大小。

1 个答案:

答案 0 :(得分:0)

我的猜测是:
您在这里拆分并尝试访问数组索引1处的元素:var temp = source.Split(FirstSeparator, StringSplitOptions.RemoveEmptyEntries)[1]; 显然,如果拆分不包含您的分隔符,则您不会拥有该元素,因此您将获得越界异常。

您应该只使用该位代码 INSIDE 来检查source变量实际上是否包含.Contains("nosyn_name_last_exact:(qxq"))


尝试:

if (line.Contains("nosyn_name_last_exact:(qxq"))
{
  string[] FirstSeparator = new string[] { "nosyn_name_last_exact:(qxq" };
  string[] SecondSeparator = new string[] { "qxq)" };
  var temp = line.Split(FirstSeparator, StringSplitOptions.RemoveEmptyEntries)[1];
  var result = temp.Split(SecondSeparator, StringSplitOptions.RemoveEmptyEntries)[0];
  result.Dump("Result: ");
}

不应该抛出