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)

Published Wednesday, July 04, 2007 2:19 PM by Palermo4
Filed under: , ,

Comments

# re: Case Insensitive String Comparison

Wednesday, July 04, 2007 2:50 PM by Juma

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

# re: Case Insensitive String Comparison

Wednesday, July 04, 2007 3:32 PM by Chris Mohan

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

# re: Case Insensitive String Comparison

Wednesday, July 04, 2007 4:28 PM by Fabrice Marguerie

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

# re: Case Insensitive String Comparison

Wednesday, July 04, 2007 6:38 PM by Will Asrari

I was thinking the same thing.

# re: Case Insensitive String Comparison

Wednesday, July 04, 2007 9:08 PM by Ian Joyce

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

# re: Case Insensitive String Comparison

Thursday, July 05, 2007 12:03 AM by Jack Yang

string.Compare() is good enough.

# re: Case Insensitive String Comparison

Thursday, July 05, 2007 4:48 AM by Patrik

Agree with the preceding speaker, totally useless. Also I'd say string.Equals(string1, string2, StringComparison.InvariantCultureIgnoreCase) would be a better choise.

# re: Case Insensitive String Comparison

Thursday, July 05, 2007 8:37 AM by Wim Hollebrandse

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... ;-)

# re: Case Insensitive String Comparison

Thursday, July 05, 2007 11:18 AM by Palermo4

Chris Mohan,

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

# re: Case Insensitive String Comparison

Thursday, July 05, 2007 11:23 AM by Palermo4

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.

# re: Case Insensitive String Comparison

Friday, July 06, 2007 3:21 AM by Jens Hofmann

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.

# re: Case Insensitive String Comparison

Friday, July 06, 2007 4:52 AM by Patrik

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.

# re: Case Insensitive String Comparison

Friday, July 06, 2007 10:29 AM by Palermo4

Jens,

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

# re: Case Insensitive String Comparison

Friday, July 06, 2007 10:34 AM by Palermo4

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.

# re: Case Insensitive String Comparison

Friday, July 06, 2007 12:50 PM by Palermo4

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.

# Utility Methods - Are They The Devil?

Tuesday, July 17, 2007 1:19 AM by Palermo4

Awhile back I posted a blog on string comparisons . The number of comments on that blog also spawned

# Revision: Case-Insensitive String Equality

Friday, July 20, 2007 2:24 PM by Palermo4

I made revisions to my method for comparing strings while ignoring case . This is in light of some good

# re: Case Insensitive String Comparison

Monday, August 27, 2007 1:49 AM by Vikram

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.

# re: Case Insensitive String Comparison

Sunday, February 10, 2008 2:17 PM by Alexander-D

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

# re: Case Insensitive String Comparison

Thursday, May 08, 2008 1:21 PM by itsmarik

Using .NET 1.1 (I know, I know, I'm behind the times), here is what I found...  If a=b, string.Equals(a, b) is fastest.  If a <> b or a.Length <> b.Length, a.Equals(b) is fastest.  string.Compare(a, b) is always slowest.  I’d suggest the following code snippet:

bool StringCompare(string a, string b)

{

   // initial "quick checks"

   if (a.Length != b.Length) { return false; }

   if (string.Equals(a, b) ) { return true ; }

   // check, case-insensitive

   return (string.Compare(a, b, true) == 0);

}

If strings are different lengths or are otherwise identical, you'd get close to string.Equals performance.  If strings are not equal, you'd get close to string.Compare performance.

# re: Case Insensitive String Comparison

Tuesday, August 05, 2008 1:16 AM by Giribabu

I want to know the difference between

== and String.Equals() in .NET

with example pls

Leave a Comment

(required) 
(required) 
(optional)
(required)