February 9, 2008 - Tags: ,

The Evils of java.lang.String.toUpperCase

String.toUpperCase is not as innocent as it looks. If you carefully read the docs you’ll see that the default signature asks for a java.util.Locale. The reasoning behind this is that there are language specific rules on how to convert lower case letters to uppercase. German, for example, has the letter “ß” which gets converted to “SS”, so “straße” becomes “STRASSE”. See the problem? The String length changed! This can trip you up if you stored it somewhere before you called toUpperCase. I’m sure there are lots of examples for other languages, so watch out and never store a String length.

(C) 2008 - Stephan Brosinski - All Rights Reserved