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);
Share:
25,335

Related videos on Youtube

Ali
Author by

Ali

3 years development, 2 years QA,

Updated on July 09, 2022

Comments

  • Ali
    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
    Neo almost 5 years
    It's worth noting that results will contain the values from array1 and not array2 with respect to case.