Skip navigation.

How to Make crossdomain.xml Work with SharePointAll recent postsBook Review: In Pursuit of Elegance

Comparing Strings Like It's 1995

While going through The Art of Unit Testing: with Examples in .NET this morning, I came across a string comparison notation, which always makes me cringe:

if (!fileName.ToLower().EndsWith (".slv"))
{
}

I’ve also lost count of seeing its closest cousin:

if (someText.ToLower() === "some test value")
{
}

This is how we used to roll in C++. I’m surprised anybody still does this.

For one, ToLower() doesn’t produce consistent results for several languages, e.g. Turkish (see The Motivation: The Turkish-I Problem).

The other problem is possibly calling ToLower() on a null, so you have to run null checks all over the place. Why not use string.Equals instead? It’s null-friendly.

In .NET, you should do culture-aware comparisons. With FxCop, every time you do otherwise, it will apply its Specify CultureInfo rule:

Because the behavior of ’string.ToLower()’ could vary based on the current user’s locale settings, replace this call in ’Foo()’ with a call to ’string.ToLower(CultureInfo)’. If the result of ’string.ToLower(CultureInfo)’ will be displayed to the user, specify ’CultureInfo.CurrentCulture’ as the ’CultureInfo’ parameter. Otherwise, if the result will be stored and accessed by software, such as when it is persisted to disk or to a database, specify ’CultureInfo.InvariantCulture’.”

Microsoft has an excellent whitepaper on MSDN, New Recommendations for Using Strings in Microsoft .NET 2.0. Read it and memorize it.

Comments

Comment permalink 1 Rik Hemsley |
Not disagreeing, but: How would this be a problem with the file extension comparison?
Comment permalink 2 Milan Negovan |
At the very least I'd do this:

fileName.EndsWith (".slv", StringComparison.OrdinalIgnoreCase)
Comment permalink 3 RichB |
ToLowerInvariant() is a better choice if you must use ToLower()
Comment permalink 4 roy |
so how would you drive such a change through TDD?
ToLower was the simplest in terms of funxtionality
Comment permalink 5 Milan Negovan |
The MSDN article I referred to has a section, "What About the Earlier Recommendation for Invariant Culture?", about ToLowerInvariant. Its usage is discourage and kept only for backward compatibility.
Comment permalink 6 net |
how about
System.Path.GetFileExtension(fileName) == ".slv"
Comment permalink 7 Milan Negovan |
You still make an assumption about casing. And what if it contains non-Latin characters? This is why you need to perform culture-aware comparisons.
Comment permalink 8 nic0428 |
Its a shame how much "==" string comparison I've used for my .net projects. Thanks for sharing the idea!! The .NET article was great.
Comment permalink 9 rtpHarry |
Interesting article. I do usually use .Equals rather than == just to avoid the possibility of accidentally assigning rather than comparing. I wasn't sure if there were any other technical reasons for this.

That book is high on my Christmas wish list as well...

Emails and Notifications

Would you like to be notified when somebody responds to this post?  Would you like to have these comments emailed to you?

Submit your comment

Please enter only text since all HTML tags except hyperlinks will be stripped. Hyperlinks will become live links. Any comments with flaming or offensive language will be deleted. Be courteous to other posters. Thank you.

Your name (required):
Your email (optional):
Your site's URL (optional):
Enter this number
Type in the number above:
Comment (required):