220

I am using the Array.Contains method on a string array. How can I make that case-insensitive?

Dirk Vollmar
  • 172,527
  • 53
  • 255
  • 316
Mike Cole
  • 14,474
  • 28
  • 114
  • 194

5 Answers5

387
array.Contains("str", StringComparer.OrdinalIgnoreCase);

Or depending on the specific circumstance, you might prefer:

array.Contains("str", StringComparer.CurrentCultureIgnoreCase);
array.Contains("str", StringComparer.InvariantCultureIgnoreCase);
Mehrdad Afshari
  • 414,610
  • 91
  • 852
  • 789
  • 5
    Per http://msdn.microsoft.com/en-us/library/dd465121.aspx, "the invariant culture has very few properties that make it useful for comparison." In almost all cases where you don't want a culture-specific comparison (`CurrentCulture`), you should be using `Ordinal` rather than `InvariantCulture`. – bdukes Jul 25 '11 at 16:58
  • 2
    @bdukes That's too strong of a statement. There are valid reasons to use `InvariantCulture`. Use of any of the three choices above depends on the circumstance. I don't object to your reordering, but I'm removing the "probably not" comment. It's already made clear in the answer that you should pick the one that works for you in the current situation. – Mehrdad Afshari Jul 25 '11 at 21:09
  • 2
    It took me a moment to realize that Contains is an extension method in System.Linq. – Craig Celeste Nov 05 '14 at 20:16
  • @MehrdadAfshari In this case the previous comment is perfectly valid. "Contains" only needs to check equality, which is more efficient with the Ordinal comparison. The other "Culture" aware comparisons are concerned with ordering of characters, and are therefore only relevant for sorting. – user1751825 Feb 20 '20 at 11:51
13

Some important notes from my side, or at least putting some distributed info at one place- concerning the tip above with a StringComparer like in:

if (array.Contains("str", StringComparer.OrdinalIgnoreCase))
{}
  1. array.Contains() is a LINQ extension method and therefore works by standard only with .NET 3.5 or higher, needing:
    using System;
    using System.Linq;

  2. But: in .NET 2.0 the simple Contains() method (without taking case insensitivity into account) is at least possible like this, with a cast:

    if ( ((IList<string>)mydotNet2Array).Contains(“str”) ) {}

    As the Contains() method is part of the IList interface, this works not only with arrays, but also with lists, etc.

Philm
  • 3,448
  • 1
  • 29
  • 28
1
new[] { "ABC" }.Select(e => e.ToLower()).Contains("abc") // returns true
Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • 5
    But wouldn't this perform a costly ToLower() call on each item in the list? String.Equals with the StringComparison overload would be better suited in this example. – Mike Cole May 19 '11 at 20:49
1

Implement a custom IEqualityComparer that takes case-insensitivity into account.

Additionally, check this out. So then (in theory) all you'd have to do is:

myArray.Contains("abc", ProjectionEqualityComparer<string>.Create(a => a.ToLower()))
Community
  • 1
  • 1
Kon
  • 27,113
  • 11
  • 60
  • 86
0

From a Powershell point of view, I like to keep things easy to read for everyone, so for:

$data = @('Zero','One','Two','Three')
$data -contains 'three' 

will do a case insensitive check but

$data -ccontains 'three'

will be case sensitive. Hope that helps.

Porky
  • 890
  • 6
  • 6