【问题标题】:Ignoring first item of string Array忽略字符串数组的第一项
【发布时间】:2013-03-28 11:57:28
【问题描述】:

如果if (item=="") 我的代码喜欢,如何忽略string[] 的第一个item

string[] temp3 = clearstring.Split(new string[]{@",","\r\n","\n","]", "\"", "}"}, StringSplitOptions.None);
temp.Close();

StreamWriter sW = new StreamWriter(Npath);

foreach (string item in temp3)
{
    if (item == "")
    {
    }

【问题讨论】:

  • 字符串为空的数组的任何实例,或者仅当数组的第一个实例为空时?
  • 是否可以忽略所有空字符串?如果是,您可以使用StringSplitOptions.RemoveEmptyEntries
  • 那么如果item是一个字符串,你怎么能使用item.First呢?
  • @MohamedSayed 对这些建议有帮助吗?
  • @MatthewWatson 查看此问题的编辑

标签: c#


【解决方案1】:

使用StringSplitOptions.RemoveEmptyEntries

string[] temp3 = clearstring.Split(new string[]{@",","\r\n","\n","]", "\"", "}"}, StringSplitOptions.RemoveEmptyEntries);

编辑: 仅输入第一个空字符串的选项:

for(int i=0; i<temp3.Length; i++)
{
    string item = temp3[i];
    if(i==0 && item == string.Empty)
       continue;

    //do work
}

【讨论】:

  • 这只是 OP 想要忽略 all 空字符串的正确方法。
【解决方案2】:

您应该使用continue 关键字。

话虽如此,您可能应该使用String.IsNullOrEmpty 方法而不是自己检查“”。

if (String.IsNullOrEmpty(item))
{
   continue;
}

【讨论】:

  • 由于字符串不能为空,最好的检查是if (item.Length == 0) 尽管可以说if (item == "") 是最易读的。 String.IsNullOrEmpty() 当然通常是最正确的(但读起来很糟糕)。
  • @MatthewWatson:它还能是空的吗?如果是这样,IsNullOrEmpty 似乎很地道。
  • @MatthewWatson:读起来并不可怕,它是惯用的。 stackoverflow.com/a/84270/195488
  • @0A0D:是的,String.IsNullOrEmpty(str) 是人们通常的做法。我只是没有碰巧发现它特别可读。 :) 但是使用扩展方法让它读起来像item.IsEmpty() 通常太麻烦了。无论如何,我要说的是,如果您知道字符串不为空,if (item.Length == 0) 表示item 不能为空。
  • @MatthewWatson:我会礼貌地不同意。读者很清楚你在做什么,并且在进行代码审查或可维护性时更容易阅读。
【解决方案3】:
string[] temp3 = clearstring.Split(new string[]{@",","\r\n","\n","]", "\"", "}"}, StringSplitOptions.None);
temp.Close();

StreamWriter sW = new StreamWriter(Npath);

int i = 0;
foreach (string item in temp3)
{
    if (item == string.empty && i == 0)
    {
        continue;
    }
    // do your stuff here
    i++;
}

【讨论】:

    【解决方案4】:

    见下文。您可以使用 continue 关键字。

    for (int i=0; i < temp3.Length; i++)
        {
                            if (i == 0 && item3[i] == "")
                            {
                               continue;
                            }
       }
    

    【讨论】:

    • 如果它是空的,你不会忽略第一个项目,而是所有空项目
    • @Natrium 并没有很好地阅读这个问题..虽然更新了我的答案。 :)
    【解决方案5】:

    为了完整起见,这里有几个 Linq 答案:

    var stringsOmittingFirstIfEmpty = temp3.Skip(temp3[0] == "" ? 1 : 0);
    
    var stringsOmittingFirstIfEmpty = temp3.Skip(string.IsNullOrEmpty(temp3[0]) ? 1 : 0);
    
    var stringsOmittingFirstIfEmpty = temp3.Skip(1-Math.Sign(temp3[0].Length)); // Yuck! :)
    

    我认为我实际上不会使用其中任何一个(尤其是最后一个,这真是个笑话)。

    我可能会去:

    bool isFirst = true;
    
    foreach (var item in temp3)
    {
        if (!isFirst || !string.IsNullOrEmpty(item))
        {
            // Process item.
        }
    
        isFirst = false;
    }
    

    或者

    bool isFirst = true;
    
    foreach (var item in temp3)
    {
        if (!isFirst || item != "")
        {
            // Process item.
        }
    
        isFirst = false;
    }
    

    甚至

    bool passedFirst = false;
    
    foreach (var item in temp3)
    {
        Contract.Assume(item != null);
    
        if (passedFirst || item != "")
        {
            // Process item.
        }
    
        passedFirst = true;
    }
    

    【讨论】:

      【解决方案6】:

      你提到如果它是空的,你需要先跳过,最后一个

      string[] temp3 = clearstring.Split(new string[]{@",","\r\n","\n","]", "\"", "}"}, StringSplitOptions.None);
      temp.Close();
      
      StreamWriter sW = new StreamWriter(Npath);
      
      for (int i=0; i< temp3.Length; i++)
      {
          if ((i == 0 || i == temp3.Length-1) && string.IsNullOrEmpty(temp3[i]))
             continue;
          //do your stuff here
      }
      

      【讨论】:

      • @Grant-Thomas 帖子:好的,我想要这个想法,因为实际上我想忽略第一项和最后一项,如果相等 "" – MohamedSayed
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-07-24
      • 1970-01-01
      • 2015-10-04
      • 1970-01-01
      • 2014-06-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多