0

I want to check if an empty string exists in an array after a specified index.

Suppose I have this array:

string[] values = {"1", "2", "2", "4", "", "", ""};

I want to check if an empty value exists from index 3 (where the value is 4). I want to do this without splitting it inter two different arrays of strings.

More specifically my example using an array named files

string[] files;

I want to check from array index 8 to array index 28 if there are any empty values.

4
  • So what was the ERROR!! you are getting? Commented Oct 14, 2014 at 5:04
  • Post your code, please. Commented Oct 14, 2014 at 5:05
  • This seems too trivial. I must have gotten it wrong. Commented Oct 14, 2014 at 5:06
  • ii want to check whether there is an empty value in the string from a specified index to end . Commented Oct 14, 2014 at 5:06

3 Answers 3

3

You can do it with LINQ:

files.Skip(index).Any(s => s == string.Empty);
Sign up to request clarification or add additional context in comments.

1 Comment

Great idea. Way more concise and (possibly) more efficient than looping.
0

You can just use a for loop. Try this:

for(int i = specifiedIndex; i < array.Length; i++)
{
    if(array[i].Equals(""))
        return true;
}
return false;

This will return true if any of the values at or after the index are empty. If it reaches the end without finding one, it returns false.

You can adjust this to fit your needs. You don't necessarily have to loop the the end of the array, you can set the end condition to a specified index too, say if you wanted to check for an empty string between indexes 5 and 10 but don't care if there are any before or after that.

Comments

0

Use LINQ:-

string[] Values = { "1", "2", "2", "4", "", "", "" };
            int specifiedIndex = 3;
            var query1 = Values.Skip(specifiedIndex).Select((v, index) => new { v, index }).Where(x => x.v == String.Empty).Select(z => z.index + specifiedIndex);

            foreach (var val in query1)
            {
                Console.WriteLine(val);
            }

3 Comments

I don't think OP needs a list of the empty strings, just whether or not one exists.
Yup...noticed that just now. I though he need all those index position where array has empty string.
Yeah I can see how it could be misread. Your answer really isn't wrong. It was one of those things where I normally wouldn't be nit picky - but I like to help avoid confusion when I can.

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.