Note: This is a question I’m asking more out of historical interest, as I realise that modern languages have built-in regular expressions and case insensitive string compare methods.
When comparing two strings of an unknown case, I can remember reading that Microsoft based conversion methods where optimized for uppercase rather than lowercase. So:
If (stringA.ToUpper() == stringB.ToUpper()) { ... }
would be quicker than:
If (stringA.ToLower() == stringB.ToLower()) { ... }
If this is true, would it be better to store string data in upper rather than lower case when you need to search it?
In .NET we could do something like the following:
if (String.Compare(stringA, stringB, StringComparison.InvariantCultureIgnoreCase) == 0) {...}
and not need to worry about turning the strings into upper or lower case. More on this here.
There is no safe case to use in the general case.
Whatever choice you make it will fail in some cases.
In the past approaches based on ToUpper and ToLower where making assumptions about working in only English text and ignoring the majority of the worlds glyphs and characters. To be more enlightened you need to use case mapping tables as the basis for case-insensitive comparisons.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With