Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Intersection of two string array (ignore case)

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" }; 
like image 448
Ali Avatar asked Apr 25 '12 20:04

Ali


1 Answers

How about an Enumerable.Intersect and StringComparer combo:

// other options include StringComparer.CurrentCultureIgnoreCase // or StringComparer.InvariantCultureIgnoreCase var results = array1.Intersect(array2, StringComparer.OrdinalIgnoreCase); 
like image 180
user7116 Avatar answered Sep 22 '22 03:09

user7116