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

前端 未结 5 406
眼角桃花
眼角桃花 2021-01-14 05:52

Right now, I have two char arrays, foo1[] and foo2[]. When I convert them to string and output to the console, they BOTH appear as

相关标签:
5条回答
  • 2021-01-14 06:10

    You are possibly looking for:

    foo2.SequenceEqual(foo1)
    
    0 讨论(0)
  • 2021-01-14 06:15

    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;
    
    0 讨论(0)
  • 2021-01-14 06:17
    string s = new string(foo1);
    string t = new string(foo2);
    
    int c = string.Compare(s, t);
    if(c==0){
        //its equal
    }
    
    0 讨论(0)
  • 2021-01-14 06:20

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

    0 讨论(0)
  • 2021-01-14 06:22
    foo1.ToList().Intersect(foo2.ToList())
    
    0 讨论(0)
提交回复
热议问题