如何在C#中获取两个字符串数组之间的差异?

时间:2021-03-04 21:32:05

How to compare two array string using C#.net? Eg:

如何使用C#.net比较两个数组字符串?例如:

string[] com1 = { "COM6", "COM7" };
string[] com2 = { "COM6", "COM7","COM8" };

Here com1 and com2 are Array string. Result: COM8. How to achieve this?

这里com1和com2是Array字符串。结果:COM8。怎么做到这一点?

6 个解决方案

#1


6  

I think this the shortest way to solve this

我认为这是解决这个问题的最短途径

foreach (string com in com2 )
{
    if (!com1.Contains(com))
    {
        MessageBox.Show(com);
    }
}

#2


51  

Sounds like you want everything in array2 except what's in array1:

听起来你想要array2中的所有东西,除了array1中的内容:

var onlyInArray2 = array2.Except(array1);

Of course, if you also wanted to know what was only in array1 you could use:

当然,如果您还想知道array1中的内容,您可以使用:

var onlyInArray1 = array1.Except(array2);

(This all requires .NET 3.5 or higher, or an alternative LINQ to Objects implementation such as LINQBridge.)

(这都需要.NET 3.5或更高版本,或LINQBridge等替代LINQ to Objects实现。)

I'm assuming that order isn't important when computing differences - Except is a set-based operator, so assumes that you're regarding the collections as sets.

我假设在计算差异时顺序并不重要 - 除了是基于集合的运算符,因此假设您将集合视为集合。

Note that Except just returns an IEnumerable<T> - if you want the results as arrays, you'll need to call ToArray:

注意,Except只返回一个IEnumerable - 如果你想把结果作为数组,你需要调用ToArray:

var onlyInArray2 = array2.Except(array1).ToArray();
var onlyInArray1 = array1.Except(array2).ToArray();

If you want the symmetric difference, i.e. you only care about which values are in a single array, rather than which array they came from, you could use:

如果你想要对称差异,即你只关心单个数组中的哪个值,而不是它们来自哪个数组,你可以使用:

var onlyInOneArray = array1.Union(array2).Except(array1.Intersect(array2));

or you could use HashSet directly:

或者您可以直接使用HashSet:

var set = new HashSet<string>(array1);
// This modifies the set...
set.SymmetricExceptWith(array2);

In all of these, the resulting order is undefined, although in practice Except will preserve the original order of the first argument. While this is strictly speaking an implementation detail, I think it's very unlikely to change.

在所有这些中,生成的顺序是未定义的,但在实践中,Except将保留第一个参数的原始顺序。虽然严格来说这是一个实现细节,但我认为它不太可能改变。

Like the other set-based operators in LINQ, Except will only return any element once - so if COM8 appeared twice in array2, it would only appear once in the result.

与LINQ中的其他基于集合的运算符一样,Except只会返回任何元素一次 - 所以如果COM8在array2中出现两次,它只会在结果中出现一次。

#3


18  

Using the Linq Except extension:

使用Linq Except扩展:

IEnumerable<string> result = com2.Except(com1); 
// result: { "COM8" }

#4


10  

You could use IEnumerable.Except.

您可以使用IEnumerable.Except。

#5


2  

If you want everything that's in one list but not both, in a single expression, you could use Union, Intersect and Except:

如果你想要一个列表中的所有内容,但不是两个,在一个表达式中,你可以使用Union,Intersect和Except:

var inOnlyOneArray = array1.Union(array2).Except(array1.Intersect(array2));

#6


1  

As others have already said, the Except extension method is definitely the way to go here. I will add, however, that it looks like you might want some control over how the comparisons are made; if 'COM8' refers to a serial port identifier, then you'd want to perform a case-insensitive comparison:

正如其他人已经说过的那样,Except扩展方法绝对是这里的方法。但是,我会补充一点,看起来你可能想要对比较的方式进行一些控制;如果'COM8'指的是串行端口标识符,那么您需要执行不区分大小写的比较:

var result = com2.Except(com1, StringComparer.OrdinalIgnoreCase);

#1


6  

I think this the shortest way to solve this

我认为这是解决这个问题的最短途径

foreach (string com in com2 )
{
    if (!com1.Contains(com))
    {
        MessageBox.Show(com);
    }
}

#2


51  

Sounds like you want everything in array2 except what's in array1:

听起来你想要array2中的所有东西,除了array1中的内容:

var onlyInArray2 = array2.Except(array1);

Of course, if you also wanted to know what was only in array1 you could use:

当然,如果您还想知道array1中的内容,您可以使用:

var onlyInArray1 = array1.Except(array2);

(This all requires .NET 3.5 or higher, or an alternative LINQ to Objects implementation such as LINQBridge.)

(这都需要.NET 3.5或更高版本,或LINQBridge等替代LINQ to Objects实现。)

I'm assuming that order isn't important when computing differences - Except is a set-based operator, so assumes that you're regarding the collections as sets.

我假设在计算差异时顺序并不重要 - 除了是基于集合的运算符,因此假设您将集合视为集合。

Note that Except just returns an IEnumerable<T> - if you want the results as arrays, you'll need to call ToArray:

注意,Except只返回一个IEnumerable - 如果你想把结果作为数组,你需要调用ToArray:

var onlyInArray2 = array2.Except(array1).ToArray();
var onlyInArray1 = array1.Except(array2).ToArray();

If you want the symmetric difference, i.e. you only care about which values are in a single array, rather than which array they came from, you could use:

如果你想要对称差异,即你只关心单个数组中的哪个值,而不是它们来自哪个数组,你可以使用:

var onlyInOneArray = array1.Union(array2).Except(array1.Intersect(array2));

or you could use HashSet directly:

或者您可以直接使用HashSet:

var set = new HashSet<string>(array1);
// This modifies the set...
set.SymmetricExceptWith(array2);

In all of these, the resulting order is undefined, although in practice Except will preserve the original order of the first argument. While this is strictly speaking an implementation detail, I think it's very unlikely to change.

在所有这些中,生成的顺序是未定义的,但在实践中,Except将保留第一个参数的原始顺序。虽然严格来说这是一个实现细节,但我认为它不太可能改变。

Like the other set-based operators in LINQ, Except will only return any element once - so if COM8 appeared twice in array2, it would only appear once in the result.

与LINQ中的其他基于集合的运算符一样,Except只会返回任何元素一次 - 所以如果COM8在array2中出现两次,它只会在结果中出现一次。

#3


18  

Using the Linq Except extension:

使用Linq Except扩展:

IEnumerable<string> result = com2.Except(com1); 
// result: { "COM8" }

#4


10  

You could use IEnumerable.Except.

您可以使用IEnumerable.Except。

#5


2  

If you want everything that's in one list but not both, in a single expression, you could use Union, Intersect and Except:

如果你想要一个列表中的所有内容,但不是两个,在一个表达式中,你可以使用Union,Intersect和Except:

var inOnlyOneArray = array1.Union(array2).Except(array1.Intersect(array2));

#6


1  

As others have already said, the Except extension method is definitely the way to go here. I will add, however, that it looks like you might want some control over how the comparisons are made; if 'COM8' refers to a serial port identifier, then you'd want to perform a case-insensitive comparison:

正如其他人已经说过的那样,Except扩展方法绝对是这里的方法。但是,我会补充一点,看起来你可能想要对比较的方式进行一些控制;如果'COM8'指的是串行端口标识符,那么您需要执行不区分大小写的比较:

var result = com2.Except(com1, StringComparer.OrdinalIgnoreCase);