2

How to ignore the first item of string[] in case of if (item=="") my code like

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 == "")
    {
    }
sloth
  • 99,095
  • 21
  • 171
  • 219
MohamedSayed
  • 147
  • 1
  • 4
  • 14

6 Answers6

3

Use the StringSplitOptions.RemoveEmptyEntries

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

Edit: Option for only ingoring first empty string:

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

    //do work
}
Rob
  • 5,578
  • 3
  • 23
  • 28
1

You should use the continue keyword.

That being said, you probably should use String.IsNullOrEmpty method instead of checking "" yourself.

if (String.IsNullOrEmpty(item))
{
   continue;
}
  • Since the string cannot be null, the best check is `if (item.Length == 0)` although arguably `if (item == "")` is the most readable. `String.IsNullOrEmpty()` is usually the most correct of course (but sadly horrible to read). – Matthew Watson Mar 28 '13 at 12:06
  • @MatthewWatson: Can it still be empty? If so, IsNullOrEmpty seems pretty idiomatic. –  Mar 28 '13 at 12:07
  • @MatthewWatson: It's not horrible to read, it's idiomatic. http://stackoverflow.com/a/84270/195488 –  Mar 28 '13 at 12:09
  • @0A0D: Yes, `String.IsNullOrEmpty(str)` is the usual way people do it. I just don't happen to find it particularly readable. :) But playing around with extension methods to make it read like `item.IsEmpty()` is generally too much hassle. Anyway, all I'm saying is that in the case that you know a string not to be null, `if (item.Length == 0)` expresses the fact that `item` cannot be null. – Matthew Watson Mar 28 '13 at 12:12
  • @MatthewWatson: I would politely disagree. It is very clear to the reader what you are doing and much easier to read when doing a code review or for maintainability. –  Mar 28 '13 at 12:13
  • @0A0D: Semantically though, `String.IsNullOrEmpty()` implies that the string may be null which is a little different (but unimportant in most cases). I will just agree to differ re: readability, since I do find `if (item == "")` easier to read than `if (string.IsNullOrEmpty(item))`. (Although that also hides the fact that `item` can't be null!) – Matthew Watson Mar 28 '13 at 12:17
  • @MatthewWatson: I agree but Microsoft decided to lump them both together since people commonly check if null and then if empty before doing anything with a String. –  Mar 28 '13 at 12:19
  • @0A0D: Agreed, and one of the reasons people do that is because of a lack of attention to Code Contracts in terms of defining when things can or can't be null, which leads to a lot of redundant null-checking. Happily, more people use Code Contracts now so this is getting better. Anyway, this is a topic for an entirely different discussion! – Matthew Watson Mar 28 '13 at 12:23
0

see below. You can use the continue keyword.

for (int i=0; i < temp3.Length; i++)
    {
                        if (i == 0 && item3[i] == "")
                        {
                           continue;
                        }
   }
scartag
  • 17,548
  • 3
  • 48
  • 52
0
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++;
}
Natrium
  • 30,772
  • 17
  • 59
  • 73
0

You mentioned that you need to skip first and last if it's empty

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
}
Menelaos Vergis
  • 3,715
  • 5
  • 30
  • 46
  • @Grant-Thomas post: ok i wanna the idea because actually i wanna to ignore the first and last item if equal "" – MohamedSayed – Menelaos Vergis Mar 28 '13 at 13:14
0

Just for completeness, here's several Linq answers:

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! :)

I don't think I'd actually use any of these (especially the last, which is really a joke).

I'd probably go for:

bool isFirst = true;

foreach (var item in temp3)
{
    if (!isFirst || !string.IsNullOrEmpty(item))
    {
        // Process item.
    }

    isFirst = false;
}

Or

bool isFirst = true;

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

    isFirst = false;
}

Or even

bool passedFirst = false;

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

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

    passedFirst = true;
}
Matthew Watson
  • 104,400
  • 10
  • 158
  • 276