Upper vs Lower Case
From Microsoft on MSDN:
Best Practices for Using Strings in the .NET Framework
Recommendations for String Usage
- Use the String.ToUpperInvariant method instead of the String.ToLowerInvariant method when you normalize strings for comparison.
Why? From Microsoft:
Normalize strings to uppercase
There is a small group of characters that when converted to lowercase cannot make a round trip.
What is example of such a character that cannot make a round trip?
- Start: Greek Rho Symbol (U+03f1) ϱ
- Uppercase: Capital Greek Rho (U+03a1) Ρ
- Lowercase: Small Greek Rho (U+03c1) ρ
ϱ , Ρ , ρ
.NET Fiddle
Original: ϱ
ToUpper: Ρ
ToLower: ρ
That is why, if your want to do case insensitive comparisons you convert the strings to uppercase, and not lowercase.
So if you have to choose one, choose Uppercase.
Based on strings tending to have more lowercase entries, ToLower should theoretically be faster (lots of compares, but few assignments).
In C, or when using individually-accessible elements of each string (such as C strings or the STL's string type in C++), it's actually a byte comparison - so comparing UPPER
is no different from lower
.
If you were sneaky and loaded your strings into long
arrays instead, you'd get a very fast comparison on the whole string because it could compare 4 bytes at a time. However, the load time might make it not worthwhile.
Why do you need to know which is faster? Unless you're doing a metric buttload of comparisons, one running a couple cycles faster is irrelevant to the speed of overall execution, and sounds like premature optimization :)
According to MSDN it is more efficient to pass in the strings and tell the comparison to ignore case:
String.Compare(strA, strB, StringComparison.OrdinalIgnoreCase) is equivalent to (but faster than) calling
String.Compare(ToUpperInvariant(strA), ToUpperInvariant(strB), StringComparison.Ordinal).
These comparisons are still very fast.
Of course, if you are comparing one string over and over again then this may not hold.
Converting to either upper case or lower case in order to do case-insensitive comparisons is incorrect due to "interesting" features of some cultures, particularly Turkey. Instead, use a StringComparer with the appropriate options.
MSDN has some great guidelines on string handling. You might also want to check that your code passes the Turkey test.
EDIT: Note Neil's comment around ordinal case-insensitive comparisons. This whole realm is pretty murky :(