Java String toLowerCase() Method
Java String class provides a method named toLowerCase() to change the case of characters in a string into lowercase letters.
Java String toLowerCase() method converts all the characters of a string into lower case, and returns that lower cased string.
In other words, the toLowerCase() method of String class returns the string in lowercase (small) letter. Nonalphabetical characters, such as digits, are are not affected by this method.
Syntax of toLowerCase() Method
Java String class provides toLowerCase() method into two flavors. The general syntax or signature of string toLowerCase() method in java is as follows below:
public String toLowerCase()
public String toLowerCase(Locale locale)
The first flavor of toLowerCase() method converts all characters of a string into lowercase letters using default locale. It does not accept any parameter.
The second flavor of toLowerCase() method converts all the characters into lowercase using the rules of specified Locale as a parameter.
String toLowerCase() Method Example Program
Let’s take a simple example program that will help to understand how to create a string and how to convert the case of characters in a string into the lowercase letter.
Example 1:
package stringProgram;
public class ChangeCase {
public static void main(String[] args)
{
// Creating a string.
String str = "I love Java Programming";
System.out.println("Original string: " +str);
// Converting string str into lowercase letters.
String toLower = str.toLowerCase();
System.out.println("Lowercase characters of string: " +toLower);
}
}
Output: Original string: I love Java Programming Lowercase characters of string: i love java programming
Let’s take another example program in which we will create a string combination of letters, digits, and symbols. In this program, we will check that non-alphabetical characters such as digits and symbols are affected by toLowerCase() method or not.
Example 2:
package stringProgram;
public class ChangeCase {
public static void main(String[] args)
{
// Creating a string.
String str = "This string is a combination of digit 08 and symbol @";
System.out.println("Original string: " +str);
// Converting string str into lowercase letters.
String toLower = str.toLowerCase();
System.out.println("Lowercase letters: " +toLower);
}
}
Output: Original string: This string is a combination of digit 08 and symbol @ Lowercase letters: this string is a combination of digit 08 and symbol @
As you can see in the output, non-alphabetical characters such as digits and symbols are unaffected by toLowerCase() method of String class.
Let’s take an example program in which we will convert string str into lowercase letter using using TURKISH and ENGLISH languages. Look at the following source code to understand better.
Example 3:
package stringProgram;
import java.util.Locale;
public class ChangeCase {
public static void main(String[] args)
{
String str = new String("I LIKE IT");
System.out.println("Original string: " +str);
// Create Locales with language "tr" for turkish and "en" for english.
Locale turkish = Locale.forLanguageTag("tr");
Locale english = Locale.forLanguageTag("en");
// Converting string str into lowercase letter using turkish and english languages.
String toLowerCaseTr = str.toLowerCase(turkish);
String toLowerCaseEn = str.toLowerCase(english);
System.out.println("Lowercase letters in turkish: " +toLowerCaseTr);
System.out.println("Lowercase letters in english: " +toLowerCaseEn);
}
}
Output: Original string: I LIKE IT Lowercase letters in turkish: ? l?ke ?t Lowercase letters in english: i like it
In this tutorial, we have covered almost all the important points related to string toLowerCase() method in Java with the help of example program. Hope that you will have understood how to change the case of characters in a string into lowercase letters.
Thanks for reading!!!