Intersection of two string array (ignore case)

Ali picture Ali · Apr 25, 2012 · Viewed 22.4k times · Source

I have two arrays:

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

I need only the matching strings in one array (ignoring case).

Result should be:

string[] result = { "blue", "black" } or { "BlUe", "black" };

Answer

user7116 picture user7116 · Apr 25, 2012

How about an Enumerable.Intersect and StringComparer combo:

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