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
