prabhakaran
prabhakaran

Reputation: 5274

How to compare the whole array for having same value

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

Answers (4)

w.b
w.b

Reputation: 11238

if (temp.All(s => s == ""))
{
}

Upvotes: 0

petchirajan
petchirajan

Reputation: 4362

Array.TruForAll method will do this.

String[] temp ;
temp got filled
if (Array.TrueForAll(temp, string.IsNullOrEmpty))
{
    //do this
}

Upvotes: 0

NullReferenceException
NullReferenceException

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

Jay Riggs
Jay Riggs

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

Related Questions