如何比较具有相同值的整个数组

本文关键字:数组 何比较 比较 | 更新日期: 2023-09-27 17:50:56

我需要检查字符串数组中的空字符串。这意味着整个数组只包含空字符串。就像

String[] temp ;
temp got filled
if(temp == "" ) // Means every member is an empty string
    //do this

有谁能说一下如何做到这一点吗?或者这是可能的吗?

*EDIT:*循环可以。但是有没有不循环的方法呢?

如何比较具有相同值的整个数组

如果字符串数组的所有元素都是空字符串,则返回true:

Array.TrueForAll(temp, s => s.Length == 0)
if (temp.All(s => s == ""))
{
}

试试这个。这里你将得到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
}

表示空字符串

var emptyStringsres= temp.Where(x => String.IsNullOrEmpty(x)).ToArray();

数组。TruForAll方法会这样做。

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