advanced web statistics

String.Compare() versus String.Equals()

12/1/2006 2:06:15 PM

I don`t know how I stumbled upon this but I can say that I remember a co-worker getting aggravated because of someone performing string comparisons using an equals sign. He was very adamant about how simply comparing two strings using an = (or == for C# ninjas out there) is the craziest thing a programmer could do. "What if your application is sold to a company in Turkey?" he asked. After reading an article this morning on string comparisons I read about an example for string comparisons that introduced a bug when used in certain cultures; namely Turkish. After brushing up on my Turkish alphabet this morning I noticed that the lowercase "i" is much different than our lowercase "i". It actually looks like a lowercase "1".

string first = "ınfinite";
string second = "Infinite";

After reading this example I smiled to myself at how right this guy was. I have in the past ignored Globalization issues because my apps have always run in English cultures.  After today I will no-longer presuppose American Operating Systems or cultures.  It is very easy to retro-fit your existing code to accomodate culture-specific string comparisons.

Before I show you how I do that, it's time for me to take a trip down memory lane of bad programming practices; at least from an academic standpoint.

The Bad
private bool ValidString(string first, string second)
{
   if (String.Equals(first.ToLower(), second.ToLower()))
      return true;
   else
      return false;
}

The Badder
private bool ValidString(string first, string second)
{
   if (String.Equals(first, second))
      return true;
   else
      return false;
}

The Better
private bool ValidString(string first, string second)
{
   if (String.Compare(first, second) == 0)
      return true;
   else
      return false;
}

What if you create an application that is sold on a world-wide scale? Maybe it is sold to cruise lines that operate out of different countries, hotel/restaurant chain, etc... There is a possibility that your application will be put onto a server with different language and culture settings. This leads me to what I believe is the best way to approach string comparisons.

The Best
private bool ValidString(string first, string second)
{
   if (String.Compare(first, second, true, CultureInfo.CurrentCulture) == 0)
      return true;
   else
      return false;
}

The two strings are passed compared, the true is a boolean value for ignoreCase. In this case we are ignoring case-sensitive (read: case-insensitivity).  This example is ideal for making sure that the input value of "bob" is the same as "Bob" or "BOB". The CultureInfo.CurrentCulture is passing in the CurrentCulture settings. Easy enough.

Proof-of-Concept
Either create a new project or add the following to an existing project.  Add this method to your code-behind.  We are going to force the Turkish culture settings here to prove this point.

private void CompareStrings()
{
   string first = "ınfinite";
   string second = "Infinite";

   if (String.Compare(first, second, true, new CultureInfo("tr-TR")) == 0)
      Response.Write("true!");
   else
      
Response.Write("false!");
}

Add a button to your form and on on it's click event handler type:
CompareStrings();

I'd love to hear your thoughts on this. Good? Bad? Could do something better?

Update
Don't forget to use the System.Globalization namespace.

Related
String.Compare Method (String, String, Boolean, CultureInfo)

C#, Code, Programming

kick it on DotNetKicks.com

Comments


Posted by: Andy Robinson | 12/1/2006 6:06:01 PM

Thanks for the heads-up Andrew. I'll have to look into that. I'm a sponge when it comes to this stuff!

Posted by: Will | 12/1/2006 6:17:47 PM

Is this c++ code or java ?

Posted by: donate car | 3/30/2007 8:43:56 AM

Its .NET you prick :)

Posted by: bitoman | 9/10/2007 8:21:35 PM

It`s recommended by the BCL team.. that String.Compare != String.Equals. I would suggest not using String.Compare.. it`s generally used for sorting algorithms.

http://blogs.msdn.com/bclteam/archive/2007/05/31/string-compare-string-equals-josh-free.aspx

Posted by: Matthew M. | 5/12/2009 6:46:38 PM

what about...

if( first.Equals( second, StringComparison.CurrentCultureIgnoreCase ) )

...vs...

using System.Globalization;
if( String.Compare( first, second, true, CultureInfo.CurrentCulture ) == 0 )

Posted by: Matt | 5/28/2009 12:45:51 PM

I am happy to find so many useful information here in the post, we need develop more strategies in this regard, thanks for sharing.

Posted by: Rehab Centers | 3/8/2010 11:43:10 AM

Leave a Comment

   

  Enter the text to proceed!