Getting string comparisons of file names right

by Michael S. Kaplan, published on 2006/08/12 03:01 -04:00, original URI:

I have spoken of the importance of using .NET 2.0's OrdinalIgnoreCase and Vista's CompareStringOrdinal for comparisons of symbolic identifiers in the past many times.

But not everyone reads this blog, of course. :-)

Just yesterday, a question that was asked kind proves that:


I was going through this link which encourages usage of StringComparison.Ordinal for filename comparisons. Basically, I would like to understand what will be the response in the following case:

filename.StartsWith(parentname, StringComparison.OrdinalIgnoreCase)

parentname: file1
filename: FILE1TEXT
current culture: Turkish

I believe, in this case it will return true. However, being a Turkish user I would like the call to fail because I is not the upper case of i. In this case how do we make the comparisons.


Well, I think I mentioned previously that the New Recommendations for Strings in .NET 2.0 were confusing. Sort of proves my point, huh? :-)

It is natural if one has expectations in one's language for one to expect that the OS will use those same rules. The only problem is that sometimes it doesn't. Especially in this case....

OrdinalIgnoreCase is the way to go in this situation. The only way to go, in fact!


This post brought to you by İ (U+0130, a.k.a. LATIN CAPITAL LETTER I WITH DOT ABOVE)

no comments

Please consider a donation to keep this archive running, maintained and free of advertising.
Donate €20 or more to receive an offline copy of the whole archive including all images.

referenced by

2007/09/23 If it isn't Unicode, it isn't ANY code!

2006/12/04 SQL and the CLR: Part 0

go to newer or older post, or back to index or month or day