How do I Compare two char[] arrays for equivalency?

孤者浪人 提交于 2019-12-01 08:57:24
string s = new string(foo1);
string t = new string(foo2);

int c = string.Compare(s, t);
if(c==0){
    //its equal
}

You are possibly looking for:

foo2.SequenceEqual(foo1)

I think you can use the SequenceEquals to compare the arrays, even though checking both lengths at first has better performance.

foo1.ToList().Intersect(foo2.ToList())

you can make a function of yours which will be faster then first converting char[] to string then compare two strings. 1. First compare length of the arrays if they are not equal return false. 2. start looping through and compare each char, If you find any diff return false else after the loop return true.

if(foo1.Length != foo2.Length){ return false;}
for(int i=0;i<foo1.Length;i++){
   if(foo1[i] != foo2[i]){ return false;}
}
return true;
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!