Failure to specify a locale when calling the methods toLowerCase()
, toUpperCase()
or format()
on
String
objects means the system default encoding will be used, possibly creating problems with international characters or number
representations. For instance with the Turkish language, when converting the small letter 'i' to upper case, the result is capital letter 'I' with a
dot over it.
Case conversion without a locale may work fine in its "home" environment, but break in ways that are extremely difficult to diagnose for customers
who use different encodings. Such bugs can be nearly, if not completely, impossible to reproduce when it’s time to fix them. For locale-sensitive
strings, the correct locale should always be used, but Locale.ROOT
can be used for case-insensitive ones.
Noncompliant code example
myString.toLowerCase()
Compliant solution
myString.toLowerCase(Locale.TR)