236

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

5 Answers 5

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

Or depending on the specific circumstance, you might prefer:

array.Contains("str", StringComparer.CurrentCultureIgnoreCase);
array.Contains("str", StringComparer.InvariantCultureIgnoreCase);
Sign up to request clarification or add additional context in comments.

4 Comments

Per 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 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.
It took me a moment to realize that Contains is an extension method in System.Linq.
@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.
14

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.

Comments

0

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()))

1 Comment

Because up until 5 minutes ago I didn't know a StringComparer existed. :)
-1
new[] { "ABC" }.Select(e => e.ToLower()).Contains("abc") // returns true

1 Comment

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.
-1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.