Java String toLowerCase() Method Examples

  • Java String toLowerCase() method converts the string into the lower case.
  • The characters in the string are converted to the lower case using the Locale rules.
  • This method is locale-specific. So the output string can be different based on the locale settings.
  • The toLowerCase() method uses Character class and standard Unicode rules to change the case of the characters.
  • Since the case mapping is not always 1:1 char mappings, the output string length may be different from the original string.
  • This method returns a new string and the original string remains unchanged.

Java String toLowerCase() Methods

There are two overloaded versions of the toLowerCase() method.

  1. toLowerCase(): uses the system default locale to convert characters to lower case.
  2. toLowerCase(Locale locale): the characters are converted to lower case using the specified locale rules.

Java String toLowerCase() Examples

Let’s look at some examples of using String toLowerCase() methods.

1. Simple String to Lower Case Conversion

jshell> Locale.getDefault()
$137 ==> en_IN

jshell> String str = "JavaString.net";
str ==> "JavaString.net"

jshell> String strLowerCase = str.toLowerCase();
strLowerCase ==> "javastring.net"

My system default locale is set to “en_IN”, so the conversion is using English locale rules.

Java String ToLowerCase Example
Java String toLowerCase() Example

2. String to Lower Case Conversion with Locale

Let’s look at an example where the default locale is different. We can easily change the default locale using the Locale class.

jshell> Locale.setDefault(new Locale("tr", "tur"));

jshell> Locale.getDefault()
$141 ==> tr_TUR

jshell> String title = "TITLE";
title ==> "TITLE"

jshell> String titleLowerCase = title.toLowerCase();
titleLowerCase ==> "tıtle"

jshell> String titleLowerCase = title.toLowerCase(new Locale("tr", "tur"));
titleLowerCase ==> "tıtle"

Notice that the titleLowerCase value is “t\u005Cu0131tle”. The Unicode code point ‘\u005Cu0131’ is the LATIN SMALL LETTER DOTLESS I character.

Java String ToLowerCase Example With Locale
Java String toLowerCase() Example With Locale

3. Locale Insensitive String to Lower Case using ROOT Locale

We can use Locale.ROOT to convert string to lower case without locale considerations.

jshell> String str = "TITLE";
str ==> "TITLE"

jshell> str.toLowerCase(new Locale("tr", "tur"));
$2 ==> "tıtle"

jshell> str.toLowerCase(Locale.ROOT);
$3 ==> "title"
Java String toLowerCase ROOT Locale
Java String toLowerCase() ROOT Locale

Conclusion

Java String toLowerCase() method is used to convert the string characters to the lower case. It’s a locale-sensitive operation. It’s recommended to use ROOT locale for locale insensitive case conversion. It’s better to pass the Locale as an argument to get a consistent result and not relying on the system default locale.


References: