I am using the Array.Contains
method on a string array. How can I make that case-insensitive?
5 Answers
array.Contains("str", StringComparer.OrdinalIgnoreCase);
Or depending on the specific circumstance, you might prefer:
array.Contains("str", StringComparer.CurrentCultureIgnoreCase);
array.Contains("str", StringComparer.InvariantCultureIgnoreCase);
-
5Per 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 usingOrdinal
rather thanInvariantCulture
.– bdukesCommented Jul 25, 2011 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. Commented Jul 25, 2011 at 21:09 -
2It took me a moment to realize that Contains is an extension method in System.Linq. Commented Nov 5, 2014 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. Commented Feb 20, 2020 at 11:51
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))
{}
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;
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.
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()))
-
20Because up until 5 minutes ago I didn't know a StringComparer existed. :)– KonCommented Jun 4, 2009 at 19:51
new[] { "ABC" }.Select(e => e.ToLower()).Contains("abc") // returns true
-
6But 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. Commented May 19, 2011 at 20:49
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.