On Avoiding ToLower

A reader writes in to ask if uppercase is OK, why isn't using ToLower?

On Avoiding ToLower
I found Bill McCarthy's article on VB's operators ["Load Up With VB's Operators," August 2007] interesting. However, I was curious why Bill asserted that it's OK to convert each string to uppercase, but you should avoid using ToLower. Can you tell me what is wrong with using ToLower, please?

Premjit (Maz) Mazumdar
received by e-mail

It depends a lot on the locale. For example, in some locales é will uppercase to E, yet E will lowercase to e. If you compare the lowercase of E to é, it won't be a match. Uppercase, on the other hand, will generally match. The use of ToUpper can be handy in circumstances like this one:

Select Case myString.ToUpper
	Case "ABC"
	Case "DEF", "FGH"
	Case "IJK"
' ...

You can also specify the culture to use:

mystring.ToUpper
(Globalization.CultureInfo.InvariantCulture)

For the most part, using ToUpper won't cause any problems, but the ToUpper call does cause a new string to be created, so it can get expensive if you use it repeatedly. With some locales, and with special characters such as the German letter ß, ToUpper won't give the same results as the Compare or Equals methods. That's why I said in the article to consider using String.Compare instead. Consider how things change if we write the ToUpper code sample using String.Compare or String.Equals:

If String.Equals(myString, "ABC",
StringComparison.OrdinalIgnoreCase) Then
ElseIf String.Equals(myString, "DEF", 
         StringComparison.OrdinalIgnoreCase) _
	OrElse String.Equals(myString, "FGH",
StringComparison.OrdinalIgnoreCase) _ 		
    Then
ElseIf String.Equals(myString, "IJK", 
   StringComparison.OrdinalIgnoreCase) Then
' ...
End If

I think I'd much rather suffer a little performance hit for the sake of readability.

--Bill McCarthy


Letters to Visual Studio Magazine are welcome. Letters must include your name, address, and daytime phone number to be considered for publication. Letters might be edited for form, fit, and style. Please send them to Letters to the Editor, c/o Visual Studio Magazine, 2600 El Camino Real, Suite 300, San Mateo, CA 94403; fax them to 650-570-6307; or e-mail them to [email protected]. Note that the views expressed in the letters section are the opinions of the letters' authors, and do not necessarily reflect the opinions of Visual Studio Magazine or those of 1105 Media.

About the Author

This story was written or compiled based on feedback from the readers of Visual Studio Magazine.

comments powered by Disqus

Featured

  • Vendors Update Controls for .NET Core 3.1, Blazor

    This week saw two third-party vendors of dev tools -- UX and UI toolkits and controls -- release new offerings that include support for two of Microsoft's main open source frameworks, the cross-platform .NET Core 3.1 and Blazor, which allows for creating browser-based web applications with C# instead of JavaScript.

  • C++ Is Focus of New Visual Studio 2019 v16.7 Preview 2

    C++ development is a focus point of the new Visual Studio 2019 v16.7 Preview 2, featuring a slew of tweaks and improvements touching upon remote SSH connections, IntelliSense support and more.

  • Clustering Non-Numeric Data Using C#

    Clustering non-numeric -- or categorial -- data is surprisingly difficult, but it's explained here by resident data scientist Dr. James McCaffrey of Microsoft Research, who provides all the code you need for a complete system using an algorithm based on a metric called category utility (CU), a measure how much information you gain by clustering.

  • So What's Up with Microsoft's (and Everyone Else's) Love of Rust?

    Microsoft already stewards several popular programming languages -- C#, TypeScript, F# -- so what's up with its love of Rust, along with the rest of the world?

  • C# Steps Up Programming Language Popularity Ladder

    Microsoft's C# programming language climbed a year-over-year notch on the TIOBE Index, which measures popularity among developers.

.NET Insight

Sign up for our newsletter.

Terms and Privacy Policy consent

I agree to this site's Privacy Policy.

Upcoming Events