LP on .NET

August 3, 2011

Case-invariant String Comparisons

Filed under: .NET,C# — Larry Parker @ 8:23 am

String comparisons are one of those things done fairly frequently in a programmer’s day job.  The fact that our alphabet has upper-case and lower-case versions of each letter makes our job a little harder since we sometimes need to perform case-invariant comparisons.  For example, we may not care about the case of the user’s last name when performing a customer lookup (although that sometimes gets resolved at the SQL level, but that’s another topic).

A simple way to compare strings without regard to case is to simply convert them to the same case.  For example:

String a = "Antwerp";
String b = "ANTWERP";

Boolean areEqual = (a.ToLower() == b.ToLower());

There’s nothing wrong with this approach (we could have used ToUpper as well), but let’s take a deeper look at what’s going on here.

The String.ToLower method has the following signature:

public string ToLower();

This means that it returns a string.  So we start off with our original string, apply the ToLower method to it, and return a new string with all characters lower case.

In our original example, we’re actually doing two ToLower operations, so two strings are returned and finally compared for equality.

Again, there’s nothing really wrong with this approach except that under the covers it seems to be doing a bit more than we need.  What we really want is to simply compare two strings for equality without regard to case.

Fortunately, there is a more efficient way.  The .NET String class provides an overload of the Equals method that lets us specify how we want to compare our string with another string.

public bool Equals(string value, StringComparison comparisonType);

One of the members of the StringComparison enum gives us just what we want, and we can rewrite our code as such:

String a = "Antwerp";
String b = "ANTWERP";

Boolean areEqual = a.Equals(b, StringComparison.OrdinalIgnoreCase);

This performs a string comparison between the two strings by using ordinal sort rules and ignoring case, and the Equals method simply returns a boolean value indicating if the comparison succeeded.  We didn’t have to convert our strings to lower-case, return the new string, and compare with the other converted string.

As a disclaimer, I have not performed benchmark tests on the two approaches to see which one is faster, nor have I inspected the .NET IL code under the covers.

But it just seems more intuitive that comparing existing strings using a designated set of rules would be a more optimal approach.

I find as I continue to work with .NET I start to think about these things a bit more.


1 Comment »

  1. […] Filed under: .NET,C# — Larry Parker @ 4:26 pm A few months back I posted about case-invariant string comparisons using the Equals method and the StringComparison […]

    Pingback by Case-Invariant Dictionaries « LP on .NET — December 27, 2011 @ 4:26 pm | Reply

RSS feed for comments on this post. TrackBack URI

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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

Create a free website or blog at WordPress.com.

%d bloggers like this: