53

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" };
icc97
  • 11,395
  • 8
  • 76
  • 90
Ali
  • 3,545
  • 11
  • 44
  • 63

1 Answers1

105

How about an Enumerable.Intersect and StringComparer combo:

// other options include StringComparer.CurrentCultureIgnoreCase
// or StringComparer.InvariantCultureIgnoreCase
var results = array1.Intersect(array2, StringComparer.OrdinalIgnoreCase);
user7116
  • 63,008
  • 17
  • 141
  • 172
  • 5
    It's worth noting that `results` will contain the values from `array1` and not `array2` with respect to case. – Neo May 28 '19 at 16:03