Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C#: is there a way to determine if a range of elements is empty?

Tags:

arrays

c#

I'm wondering if theres a method to determine whether a certain range of array elements is empty or not. for example, if the array was initialized with 10 elements having the values "", then if data was later assigned to elements 5, 7, 9; could i test if elements 0-3 were empty, or rather contained an empty string ""?

like image 901
Sinaesthetic Avatar asked Dec 22 '22 22:12

Sinaesthetic


2 Answers

array.Skip(startIndex).Take(count).All(x => string.IsNullOrEmpty(x));

So, if you are trying to check elements 0-3:

array.Skip(0).Take(4).All(x => string.IsNullOrEmpty(x));

For clarity, I left Skip in there.

Edit: made it Take(4) instead of 3 as per Jonathan's comment in the other answer (and now Guffa's comment in mine. ;) ).

Edit 2: According to comments below, the OP wanted to see if any of the elements matched:

array.Skip(0).Take(4).Any(x => string.IsNullOrEmpty(x));

So changed All to Any.

like image 171
Kirk Woll Avatar answered Dec 24 '22 11:12

Kirk Woll


bool is0to3empty = myArrayOfString.Skip(0).Take(4).All(i => string.IsNullOrEmpty(i));
like image 35
Rex M Avatar answered Dec 24 '22 12:12

Rex M