Intersection of two string array (ignore case)
25,335
How about an Enumerable.Intersect
and StringComparer
combo:
// other options include StringComparer.CurrentCultureIgnoreCase
// or StringComparer.InvariantCultureIgnoreCase
var results = array1.Intersect(array2, StringComparer.OrdinalIgnoreCase);
Related videos on Youtube
Comments
-
Ali almost 2 years
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" };
-
Neo almost 5 yearsIt's worth noting that
results
will contain the values fromarray1
and notarray2
with respect to case.