Peter Petrov’s Weblog

var me = from practice in programming where practice.IsBestPractice && practice.UseLambda select practice.OptimalPerformance;

Useful method – 6 of N – Ignore case on String.Contains() June 27, 2008

Filed under: .NET Framework,C# — ppetrov @ 12:39 pm
Tags: , , ,

The easiest way to see if a String contains another string is to use the method Contains().

The documentation has Remarks

This method performs an ordinal (case-sensitive and culture-insensitive) comparison. The search begins at the first character position of this string and continues through the last character position.

But what if we want a case-insensitive comparison? The answer is simple – we can use IndexOf(). The problem is the name of the method isn’t as intuitive as Contains() is(Contains calls IndexOf under the hood). To create a little comfort I’ve wrapped the IndexOf() call to an extension method which will determine if a string contains another string ignoring the case.

public static bool Contains(this string original, string value, StringComparison comparisionType)
    return original.IndexOf(value, comparisionType) >= 0;

In fact we pass StringComparison as parameter so it’s easy to specify the culture and the case.


8 Responses to “Useful method – 6 of N – Ignore case on String.Contains()”

  1. Thanks – saved a few minutes 🙂

  2. Tyler Collier Says:

    Great idea, thanks. I was trying to use System.Linq.Enumerable.Contains without success. I was trying e.g. “myString”.Contains(“value”, StringComparer.OrdinalIgnoreCase) but it wouldn’t compile with error message: “‘string’ does not contain a definition for ‘Contains’ and the best extension method overload ‘System.Linq.Queryable.Contains(System.Linq.IQueryable, TSource, System.Collections.Generic.IEqualityComparer)’ has some invalid arguments.”

  3. Patrick Says:

    Thanks! Very helpful.

  4. weitzhandler Says:

    Great idea,
    Thank u very much

  5. Ruthie Says:

    thank you so very much !

  6. You extension method works nice, but in some cases there will be a strange null reference exception in the contains itself. Wouldn’t it be better to check if the ‘original’ is null?

    public static bool Contains(this string original, string value, StringComparison comparisionType)
    if (original == null)
    return false;

    return original.IndexOf(value, comparisionType) >= 0;

    • ppetrov Says:

      Yeah, sure. You are absolutely right but I prefer to throw an Argument NullException in this case in order to avoid “strange null reference exception”. I will change the method implementation in the post. Thanks for comment.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s