54

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" };

1 Answer 1

108

How about an Enumerable.Intersect and StringComparer combo:

// other options include StringComparer.CurrentCultureIgnoreCase
// or StringComparer.InvariantCultureIgnoreCase
var results = array1.Intersect(array2, StringComparer.OrdinalIgnoreCase);
1
  • 6
    It's worth noting that results will contain the values from array1 and not array2 with respect to case.
    – Neo
    Commented May 28, 2019 at 16:03

Not the answer you're looking for? Browse other questions tagged or ask your own question.