Case Insensitive String Comparison

Here is a little utility method that takes two strings and determines equality while ignoring case.  Thus "silverlight"=="SilverLight"=="SILVERLIGHT"==true.

public static class Strings
{
    public static bool IsEqual(string string1, string string2)
    {
        return (string.Compare(string1, string2, true) == 0);
    }
}
UPDATE (2007-07-05)
Based on feedback I received (thank you) I now favor the following line for case insensitive string comparison:

firstString.Equals(secondString, StringComparison.InvariantCultureIgnoreCase)

19 Comments

  • You probably want to use StringComparison.InvariantCultureIgnoreCase as your third parameter instead of IgnoreCase(true) to make the function work across cultures.

  • Why not just use the built-in over-ride for the string's Equals method that accepts a StringComparison enum item.

    ie:
    bool isEqual = string1.Equals( string2, StringComparison.OrdinalIgnoreCase );

    The StringComparison enum allows you to specify the following comparison options:
    StringComparison.CurrentCulture
    StringComparison.CurrentCultureIgnoreCase
    StringComparison.InvariantCulture
    StringComparison.InvariantCultureIgnoreCase
    StringComparison.Ordinal
    StringComparison.OrdinalIgnoreCase

  • What is the benefit of introducing a new method while you already have the one you use (String.Compare) or String.Equals?

  • I was thinking the same thing.

  • And with a name like IsEqual it's obviously case insensitive.

  • string.Compare() is good enough.

  • Agree with Fabrice's comment. This utility function is a utility function for the sake of it.

    Just wondering if you're going to create an IsEqual overload which accepts CultureInfo as well? Or maybe even a boolean flag to control whether the comparison should be case sensitive or insensitive... ;-)

  • Chris Mohan,

    Thanks for the feedback! I did not know the Equals on a string instance had that enumerated value.

  • Although I like the "instance".Equals("INSTANCE", StringComparison.InvariantCultureIgnoreCase) method, I disagree with comments that the method in my post is worthless. The Compare method returns an int, not a bool. My method served two purposes:
    1) Eliminate one argument
    2) Return a bool, not a number

    For use and support, it does serve a purpose. Nevertheless, I do like the string instance equals method, since it does accomplish the same thing.

  • The difference between string.Compare() and .Equals is that string.compare accepts null-values for both parameters. When using instance-equals-method you'll get a null reference exception when the instance is null and you want to compare the values. So string.compare is one statement and .equals requires two statements to be safe (check for null prev.). So I think the wrapper method of the blog-author does make sense! So dont be so fast in criticising. It's a matter of the usage of the string-compare logic.

  • It does eliminate an argument as you say, but it does it in a way that makes it hard to understand. The method is called IsEqual (should be AreEqual) but it doesn't determine equality, therefore it should be renamed to something like "AreCaseInsensitiveEqual". Now with this method you'd have to import (using-directive) the namespace where the "Strings" static class is located and then type "Strings.AreCaseInsensitiveEqual(s1, s2)" instead of no import and simply typing string.Equals(s1, s2, StringComparison.InvariantCultureIgnoreCase), ofcourse assisted by intellisense.

    As far as returning a bool, not a number that's what the string.Equals-method returns.

  • Jens,

    You are absolutely correct. I did not consider the "null" factor. Thanks for the feedback.

  • Patrik,

    Good points. The method name could be better. Perhaps Strings.AreEqualIgnoresCase?

    And regarding string.Equals(...), I agree with you. That is why I updated the blog entry to make the user aware. The StringComparison.InvariantCultureIgnoreCase enum is new to 2.0. Until the comments on this blog, I had not seen it before. However, in light of Jens comment, there may be an advantage to still maintaining a utility method for the sake of resolving null values.

  • Jack,

    I forgot to respond to your comment about string.Compare() is good enough. Once again, Compare returns an int, not a bool. Compare is also intended for use with sorting, not specifically for equlity. If I want to know if two strings are equal, I want a true or false response, not -1, 0, 1.

  • I would be rather adding an extension method on the string class that does the job for me. You see this the perfect example of the usages of extension method.

  • In Perl, to ignore case, you add the letter 'i' to the match operator. That's a bit easier than "StringComparison.InvariantCultureIgnoreCase", don't you think?

    The word "equals" comes from mathematics, and people shouldn't be abusing the word "equals" to mean "is similar to."

  • I want to know the difference between
    == and String.Equals() in .NET
    with example pls

  • Using an equals sign:
    string myVariable = "This text";
    is the same as
    myVariable.Equals("This text");

    Just that you have to have already declared "myVariable" to make the second one work.

    This is what I think people will really be after (I was, anyway...). If you want to make something case-insensitive, do a conditional statement like this:

    string myVariable = "This text";
    string thisOtherValue = "tHis TExT";

    if (myVariable.Equals(thisOtherValue, StringComparison.InvariantCultureIgnoreCase))
    // do the stuff that would have been done here
    // had they exactly equaled each other by case

    Enjoy,
    Tom

  • just wanted tos ay thanks to all your discussions! As a beginner, It made my ASP.net life easier .

Comments have been disabled for this content.