Reputation: 5274
I am in the need of checking a string array for empty strings. That means the whole array contains only empty strings. Something like
String[] temp ;
temp got filled
if(temp == "" ) // Means every member is an empty string
//do this
Can anybody say how to achieve this? Or is it possible?
*EDIT:*Looping is OK. But is there any way possible without looping?
Upvotes: 0
Views: 98
Reputation: 4362
Array.TruForAll method will do this.
String[] temp ;
temp got filled
if (Array.TrueForAll(temp, string.IsNullOrEmpty))
{
//do this
}
Upvotes: 0
Reputation: 1639
Try this.Here you will get valid strings in res
string[] temp = new string[] { "", "abc", "axyz" };
var res= temp.Where(x => !String.IsNullOrEmpty(x)).ToArray();
if(res.Count()>0)
{
.....//temp contains atleast one valid string
}
if(res.Count()==0)
{
.....//All strings in temp are empty
}
for empty strings
var emptyStringsres= temp.Where(x => String.IsNullOrEmpty(x)).ToArray();
Upvotes: 0
Reputation: 53593
This will return true if all elements of an array of strings are empty strings:
Array.TrueForAll(temp, s => s.Length == 0)
Upvotes: 3