加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – 如何比较两个char []数组的等效性?

发布时间:2020-12-15 19:46:30 所属栏目:百科 来源:网络整理
导读:现在,我有两个char数组,foo1 []和foo2 [].当我将它们转换为字符串并输出到控制台时,它们同时显示为条形.我知道我可以这样做: int g;for (int i=0;ilength.foo1;i++) { // loop from 0 to length of array if (foo1[i]=foo2[i]) { // if foo1[0] and foo2[0]
现在,我有两个char数组,foo1 []和foo2 [].当我将它们转换为字符串并输出到控制台时,它们同时显示为条形.我知道我可以这样做:

int g;
for (int i=0;i<length.foo1;i++) {      // loop from 0 to length of array
    if (foo1[i]=foo2[i]) {             // if foo1[0] and foo2[0] are the same char
    g++;                               // increment a counter by 1
}
else                                   // otherwise...
{
    i=100;                             // set i to something that will halt the loop
}
if (g = length.foo1) {                 // if the incremented counter = length of array
    return true;                       // arrays are equal,since g only increments
}                                      // in the case of a match,g can ONLY equal the
else {                                 // array length if ALL chars match
    return false;                      // and if not true,false.

逐字逐句比较,但我猜这是一种更简单的方法.有趣的是,我所做的大部分谷歌用于比较char []的eqivalency c#和类似的关键字导致了大量关于比较STRING数组的信息,或者当数组的PART匹配某些东西时比较字符串或char数组……我们无法找到任何关于测试char数组的简单方法.踩过每个阵列是最好的方法吗?或者有没有办法比较foo1 = foo2或foo1 == foo2?这些都不适合我.

基本上,我需要测试char foo1 []和char foo2 []是否同时为{B,A,R}

解决方法

string s = new string(foo1);
string t = new string(foo2);

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读