C# 忽略字符串数组的第一项

C# 忽略字符串数组的第一项,c#,C#,如果是if(item==“”)我的代码,如何忽略string[]的第一个项 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

如果是
if(item==“”)
我的代码,如何忽略
string[]
的第一个

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 == "")
    {
    }

您应该使用
continue
关键字

也就是说,您可能应该使用方法,而不是自己检查“”

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

您应该使用
continue
关键字

也就是说,您可能应该使用方法,而不是自己检查“”

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

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

for (int i=0; i < temp3.Length; i++)
    {
                        if (i == 0 && item3[i] == "")
                        {
                           continue;
                        }
   }
for(int i=0;i
见下文。您可以使用continue关键字

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

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

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

    //do work
}
对于(int i=0;i使用

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

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

    //do work
}

for(int i=0;i您提到,如果它为空,则需要跳过第一个和最后一个

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
}
string[]temp3=clearstring.Split(新字符串[]{@“,”,“\r\n”,“\n”,“]”,“\”,“}”},StringSplitOptions.None);
温度关闭();
StreamWriter sW=新StreamWriter(Npath);
for(int i=0;i
您提到,如果文件为空,则需要跳过“第一个”和“最后一个”

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
}
string[]temp3=clearstring.Split(新字符串[]{@“,”,“\r\n”,“\n”,“]”,“\”,“}”},StringSplitOptions.None);
温度关闭();
StreamWriter sW=新StreamWriter(Npath);
for(int i=0;i
为了完整起见,这里有几个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 passedFirst = false;

foreach (var item in temp3)
{
    Contract.Assume(item != null);

    if (passedFirst || item != "")
    {
        // Process item.
    }

    passedFirst = true;
}

为了完整起见,这里有几个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 passedFirst = false;

foreach (var item in temp3)
{
    Contract.Assume(item != null);

    if (passedFirst || item != "")
    {
        // Process item.
    }

    passedFirst = true;
}


字符串为空或数组的第一个存在为空的数组实例?是否可以忽略所有空字符串?如果可以,您可以使用
StringSplitOptions.RemoveEmptyEntries
。因此,如果
item
是字符串,您如何使用
item.first
?@MohamedSayed对这些建议是否有把握?@MatthewWatson查看此问题的编辑字符串为空的数组的任何实例,或者如果数组的第一个存在为空,是否可以忽略所有空字符串?如果可以,您可以只使用
StringSplitOptions.RemoveEmptyEntries
。因此,如果
item
是字符串,如何使用
item.first
?@MohamedSayed a这些建议你运气好吗?@MatthewWatson看到这个问题的编辑了吗?因为字符串不能为空,最好的检查方法是
if(item.Length==0)
,尽管可以说
if(item==”)
是最可读的。
string.IsNullOrEmpty()
通常是最正确的(但读起来很糟糕)@MatthewWatson:它还能是空的吗?如果是这样的话,IsNullOrEmpty看起来很地道。@MatthewWatson:读起来并不可怕,它很地道。@0A0D:是的,
String。IsNullOrEmpty(str)
是人们通常的做法。我只是碰巧觉得它不是特别可读。:)但是使用扩展方法使其读起来像
item.IsEmpty()
通常太麻烦了。无论如何,我要说的是,如果知道字符串不能为null,
if(item.Length==0)
表示
不能为空。@MatthewWatson:我有礼貌地不同意。读者很清楚你在做什么,并且在进行代码审查或维护时更容易阅读。由于字符串不能为空,最好的检查方法是
if(item.Length==0)
尽管可以说
if(item==“”)
是最可读的。
String.IsNullOrEmpty()
通常是最正确的(但读起来很糟糕)。@MatthewWatson:它仍然是空的吗?如果是这样,IsNullOrEmpty看起来很地道。@MatthewWatson:读起来并不可怕,它很地道。@0A0D:是的,
String.IsNullOrEmpty(str)
是人们通常使用的方法。我只是不觉得它特别可读。:)但是玩弄扩展方法使它看起来像
item.IsEmpty()
通常太麻烦了。总之,我想说的是,如果你知道字符串不能为空,
如果(item.Length==0)
表示
不能为空。@MatthewWatson:我有礼貌地不同意。读者很清楚你在做什么,并且在进行代码审查或维护时更容易阅读。如果第一项为空,你不会忽略它,而是全部为空items@Natrium我没把问题读清楚。。更新了我的答案。:)如果第一项为空,则不会忽略它,而是全部为空items@Natrium我没把问题读清楚。。更新了我的答案。:)这是唯一正确的OP想要忽略所有空字符串。这是唯一正确的OP想要忽略所有空字符串。@Grant Thomas post:好的,我想要这个主意,因为实际上我想要忽略第一个和最后一个相等的项目“” – MohamedSayed@Grant-托马斯·波斯特:好的,我想要这个想法,因为实际上我想忽略第一个和最后一个相等的项目