检查是否相等两个字符串数组 [英] Examining two string arrays for equivalence

查看:102
本文介绍了检查是否相等两个字符串数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有更好的方法来检查两个字符串数组是否具有相同的内容比这?



 的String []首先=新的String [] {猫,和,老鼠}; 
的String []秒=新的字符串[] {猫,和,老鼠};

布尔contentsEqual = TRUE;

如果(first.Length == second.Length){
的foreach(字符串s第一)
{
contentsEqual&安培; = second.Contains(S) ;
}
}
,否则{
contentsEqual = FALSE;
}


Console.WriteLine(contentsEqual.ToString()); //真正的


解决方案

Enumerable.SequenceEquals 如果他们应该是在相同的顺序。


Is there a better way to examine whether two string arrays have the same contents than this?

string[] first = new string[]{"cat","and","mouse"};
string[] second = new string[]{"cat","and","mouse"};

bool contentsEqual = true;

if(first.Length == second.Length){
    foreach (string s in first)
    {
        contentsEqual &= second.Contains(s);
    }
}
else{
    contentsEqual = false;
}


Console.WriteLine(contentsEqual.ToString());//    true

解决方案

Enumerable.SequenceEquals if they're supposed to be in the same order.

这篇关于检查是否相等两个字符串数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆