The String.toLowerCase() returns a new string after converting all the characters in a given string to lowercase using the Locale rules if specified. Note that the length of the original string and lowercase string may be different due to a few locale-specific characters.
1. String.toLowerCase() API
The toLowerCase() method optionally accepts the Locale instance that should be used for locale-sensitive case conversion. If not specified, the default Locale rules are applied.
public String toLowerCase();
public String toLowerCase(Locale locale);
2. String.toLowerCase() Example
The following Java program converts the string to lowercase using default locale rules. The default Locale is obtained using the Locale.getDefault() method.
String name = "HowToDoInJava.com";
Assertions.assertEquals("howtodoinjava.com", name.toLowerCase());
3. Locale-Specific Case Conversion
Java program to convert the string to lowercase using default locale rules.
System.out.println("hello world".toLowerCase(Locale.getDefault()));
System.out.println("Γειά σου Κόσμε".toLowerCase(Locale.US));
Program output.
hello world
γειά σου κόσμε
In this example, we learned to convert string to lowercase.
Happy Learning !!
References: String Java Doc
Comments