0

我有两个字符串数组名称 str_arr1[],str_arr2[]。具有相同或不同值的两个数组。

str_arr1[] = { "one", "two", "three","four","Doctor","Engineer","Driver" };
str_arr2[] = { "one", "Doctor","Engineer" };

通常 str_arr1[] 与 str_arr2[] 相比具有更多记录。我想检查具有 str_arr2[] 的 str_arr1[]。如果它是真的意味着返回真。否则返回假。

4

7 回答 7

3
function compareArrays(arr1, arr2) {
  if (arr1.length != arr2.length) {
    return false;
  }

  return arr1.sort().join() == arr2.sort().join();
}
var arr1 = ["one", "two", "three","four","Doctor","Engineer","Driver" ];
var arr2 = ["one", "Doctor","Engineer"];

    console.log(compareArrays(arr1,arr2));
于 2013-09-16T06:17:38.327 回答
3
bool contains = !str_arr2.Except(str_arr1).Any();
于 2013-09-16T06:09:38.790 回答
2

请使用这个

static bool ArraysEqual<T>(T[] a1, T[] a2)
{
    if (ReferenceEquals(a1,a2))
        return true;

    if (a1 == null || a2 == null)
        return false;

    if (a1.Length != a2.Length)
        return false;

    EqualityComparer<T> comparer = EqualityComparer<T>.Default;
    for (int i = 0; i < a1.Length; i++)
    {
        if (!comparer.Equals(a1[i], a2[i])) return false;
    }
    return true;
}
于 2013-09-16T06:08:34.850 回答
1
bool result=str_arr2.Any(x=>!str_arr1.Contains(x));
于 2013-09-16T06:11:08.670 回答
0

你没有清除条件。我正在结合所有条件。

 string[] str_arr1 = new string[] { "one", "two", "three", "four", "Doctor", "Engineer", "Driver" };
 string[] str_arr2 = new string[] { "one", "aaaa", "yyy" };

 bool contains = !str_arr2.Except(str_arr1).Any(); // this will show true only if all the items of str_arr2 are in str_arr1 

 bool result = str_arr2.Any(x => !str_arr1.Contains(x));// this will show true if any of the items of str_arr2 are in str_arr1 
于 2013-09-24T12:54:38.343 回答
0
var str_arr1 = new string[] { "one", "two", "three", "four", "Doctor", "Engineer", "Driver" };
        var str_arr2 = new string[] { "one", "Doctor", "Engineer" };

        return str_arr1.Intersect(str_arr2).Count() == str_arr2.Length;
于 2013-09-16T06:11:47.067 回答
0
var inter = str_arr1.Intersect( str_arr2);

foreach (var s in inter)
{
    Console.WriteLine("present" + s); // or you can return somthin
}
于 2013-09-16T06:12:59.657 回答