Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What case to use when comparing strings in Microsoft programming languages?

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?

like image 379
woodstylee Avatar asked Feb 26 '26 19:02

woodstylee


2 Answers

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.

like image 65
jpoh Avatar answered Mar 01 '26 11:03

jpoh


There is no safe case to use in the general case.

Whatever choice you make it will fail in some cases.

  • Some languages have no case (not really a problem).
  • Some languages have a third "title" case.
  • Some characters do not round trip, e.g. ToUpper("ß") is "SS", and ToLower("SS") is "ss", but there are some words only distingished by "ß" vs "ss" so will give a false positive is matched by mapping to upper case (and which will break assumptions about case mapping not changing string lengths).
  • Case mapping is language dependent. E.g. ToLower("I") is "i" unless you have working in Turkish or Azari where the result is "ı" (Latin Small Letter Dotless I) and ToUpper("i") is "İ" (Latin Capital Letter I With Dot Above).

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.

like image 44
Richard Avatar answered Mar 01 '26 12:03

Richard



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!