1. Introduction
In this article, You'll be learning
how to convert each character in the string into a lower case.
This is very easy and can be done using
String class toLowerCase() method which returns a new String after converting every character to the lowercase.
2. Java String toLowerCase() Syntax
Below is the syntax from
java api documentation.
public String toLowerCase()
public String toLowerCase(Locale locale)
This method does not take any argument but returns a new String with lowercase contents.
How to convert String to lowercase?
3. Java String toLowerCase() Example - To convert String to Lowercase
package com.javaprogramto.w3schools.programs.string;
public class StringToLowercase {
public static void main(String[] args) {
String string = "HELLO WELCOME TO THE JAVAPROGRAMTO.COM";
String lowercaseStr = string.toLowerCase();
System.out.println("Lowercase converted string : "+lowercaseStr);
}
}
Output:
Lowercase converted string : hello welcome to the javaprogramto.com
4. Java String toLowerCase() Locale Examples
By default, toLowerCase() does conversion internally based on the default locale from the computer or server.
toLowerCase(Locale.getDefault());
I have set the default locale as English in my machine so conversion is done for this locale;
But, if you want to for different locale then use the toLowerCase(Locale locale) overloaded method instead of the
toLowerCase() method.
package com.javaprogramto.w3schools.programs.string;
import java.util.Locale;
public class StringToLowercase {
public static void main(String[] args) {
Locale defaultLocale = Locale.getDefault();
System.out.println("Default locale : "+defaultLocale);
String string = "I am Now Using the different locales";
Locale french = Locale.forLanguageTag("co-FR");
System.out.println("french loale : "+french);
String lowercaseStr = string.toLowerCase(french);
System.out.println("Lowercase converted string in French : "+lowercaseStr);
Locale chineesLocale = new Locale("zh", "CN");
String chineeslowerCase = string.toLowerCase(chineesLocale);
System.out.println("Chinees locale string : "+chineeslowerCase);
}
}
Output:
Default locale : en_US
french loale : co_FR
Lowercase converted string in French : i am now using the different locales
Chinees locale string : i am now using the different locales
5. Java String toLowerCase() Internal Implementation
public String toLowerCase() {
return toLowerCase(Locale.getDefault());
}
public String toLowerCase(Locale locale) {
return isLatin1() ? StringLatin1.toLowerCase(this, value, locale)
: StringUTF16.toLowerCase(this, value, locale);
}
6. Conclusion
In this article, We've seen how to convert the string to lowercase with the default locale and with custom locale for specified country.
All examples showed are over
GitHub.