Wednesday, February 5, 2014

String Comparison (C#.NET)




·         CurrentCultureIgnoreCase
·         InvariantCultureIgnoreCase
·         OrdinalIgnoreCase

OrdinalIgnoreCase is fastest, but doesn’t work with Turkish (and maybe some other languages). Turkish has two letters for “I”, one with dot (i) and one without. In English we have just one and the lowercase has dot and uppercase doesn’t.

For testing string equality, InvariantIgnoreCase is equivalent to OrdinalIgnoreCase (but may be a bit slower). For sorting they are different.

When comparing equality of programmatic values, OrdinalIgnoreCase is best. One thing seems wrong in the MS doc, though. “This is most appropriate when comparing strings that are generated programmatically or when comparing case-insensitive resources such as paths and filenames.” Paths and file names could have the Turkish “I”.

String.Compare("İ", "i", new System.Globalization.CultureInfo("tr-TR"), System.Globalization.CompareOptions.IgnoreCase) == 0

String.Compare("İ", "i", new System.Globalization.CultureInfo("tr-TR"), System.Globalization.CompareOptions.OrdinalIgnoreCase) != 0


When comparing localized or user-authored text, use CurrentCultureIgnoreCase.

This article is pretty good on the InvariantCulture. It’s best for when consistent values are needed across cultures, such as when persisted.

If you want to compare and ignore accents, for example, so that “resume” and “Résumé” are considered equal, use String.Compare.

String.Compare("resume", "Résumé", System.Globalization.CultureInfo.CurrentCulture, System.Globalization.CompareOptions.IgnoreCase | System.Globalization.CompareOptions.IgnoreNonSpace) == 0

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.