两个字符串数组的交集(忽略大小写)

53

我有两个数组:

string[] array1 = { "Red", "blue", "green", "black" };
string[] array2 = { "BlUe", "yellow", "black" };

我需要在一个数组中仅匹配字符串(忽略大小写)。

结果应该是:

string[] result = { "blue", "black" } or { "BlUe", "black" };
1个回答

105

可以试试使用 Enumerable.IntersectStringComparer 的组合:

// other options include StringComparer.CurrentCultureIgnoreCase
// or StringComparer.InvariantCultureIgnoreCase
var results = array1.Intersect(array2, StringComparer.OrdinalIgnoreCase);

5
值得注意的是,相对于大小写而言,“结果” (results) 将包含来自 array1 而非 array2 的值。 - Neo

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接