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 == "")
    {
    }
5
  • 3
    Any instance of the array where the string is empty or just if the first isntance of the array is empty? Commented Mar 28, 2013 at 11:58
  • 4
    Is it OK to ignore all empty strings? If yes, you could just use StringSplitOptions.RemoveEmptyEntries. Commented Mar 28, 2013 at 12:02
  • So if item is a string, how can you use item.First? Commented Mar 28, 2013 at 12:25
  • @MohamedSayed any luck with the suggestions? Commented Mar 28, 2013 at 12:43
  • @MatthewWatson See the edits of this question Commented Mar 28, 2013 at 12:56

6 Answers 6

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
}
Sign up to request clarification or add additional context in comments.

1 Comment

This is only correct the OP wants to ignore all empty strings.
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;
}

8 Comments

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).
@MatthewWatson: Can it still be empty? If so, IsNullOrEmpty seems pretty idiomatic.
@MatthewWatson: It's not horrible to read, it's idiomatic. stackoverflow.com/a/84270/195488
@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.
@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.
|
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++;
}

Comments

0

see below. You can use the continue keyword.

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

2 Comments

you're not ignoring the first item if it is empty, but all empty items
@Natrium Didn't really read the question well .. updated my answer though. :)
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;
}

Comments

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
}

1 Comment

@Grant-Thomas post: ok i wanna the idea because actually i wanna to ignore the first and last item if equal "" – MohamedSayed

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.