The “toLowerCase()” method is available in the String class of the “java.util” package. It returns the same String after converting each String character from uppercase to lowercase. The “toLowerCase()” method allows users to enter their details regardless of the case sensitivity. This method is useful for “User Authentication”, “Database Operations”, “Web Development” and many more.
This guide explains the usage of the String toLowerCase() method in Java.
How to Use Java String toLowerCase() Method?
The “toLowerCase()” method is applied over the targeted String to convert all characters of that string into lowercase. This method has two variants, the first one performs a simple conversion into lowercase format. The second variant accepts the “Locale” as an argument which converts the String according to the provided Locale region.
Syntax
The syntax of the “toLowerCase()” method for both discussed variants is shown below:
//first variant
public String toLowerCase()
//second variant
public String toLowerCase(Locale loc)
In the above syntax, the Locale type parameter can store Locale for Turkish, Lithuanian, and many more.
Let’s dive into the practical demonstration of these methods by following the below-mentioned examples.
Example 1: Use of String toLowerCase() Method in Java
In this example, the “toLowerCase()” method is applied over multiple Strings to set their cases into lowercase:
class javabeat{
public static void main(String args[]) {//Initializing the main() method
String str1 = "Learn Java With JavaBeat";
String str2 = "It's javaBEAT";
String str3 = "234Java@Beat";
//Converting Strings to Lowercase
String convertStr1 = str1.toLowerCase();
String convertStr2 = str2.toLowerCase();
String convertStr3 = str3.toLowerCase();
System.out.println("\nThe '" + str1 + "' is converted to Lowercase as: \n" + convertStr1);
System.out.println("\nThe '" + str2 + "' is converted to Lowercase as: \n" + convertStr2);
System.out.println("\nThe '" + str3 + "' is converted to Lowercase as: \n" + convertStr3);
}
}
The above code works like this:
- First, the class “javabeat” containing the “main()” method is created. Inside the method, three String type variables “str1”, “str2”, and “str3” are declared.
- Then, the “toLowerCase()” method is applied to the already created String type variables. The results of these methods are stored in the “convertStr1”, “convertStr2”, and “convertStr3” variables.
- Finally, the “System.out.println()” method is used to display the “convertStr1”, “convertStr2”, and “convertStr3” variables on the console.
Output for the above code snippet confirms the conversion of Strings into lowercase format:
Example 2: Comparing Case In-Sensitive Strings Using String toLowerCase() Method
The “toLowerCase()” method is also useful in comparing the Strings irrespective of their case convention, i.e., uppercase or lowercase. It does this by applying the mentioned method over both Strings to convert all characters into a lowercase format. After that, the corresponding comparison method is applied to the desired operation. The code is shown as:
class javabeat {
public static void main(String args[]) {//Initializing the main() method
String str1 = "Learn Java With JavaBeat";
String str2 = "It's javaBEAT";
String str3 = "it's javabeat";
//Converting Strings Cases to Lowercase
String convertStr1 = str1.toLowerCase();
String convertStr2 = str2.toLowerCase();
String convertStr3 = str3.toLowerCase();
//Comparing the Lowercase String
boolean result1 = convertStr1.equals(convertStr2);
boolean result2 = convertStr2.equals(convertStr3);
//Displaying the Comparison Result
System.out.println("\nThe comparison result for '" + str1 + "' and '" + str2 + "' is: \n" + result1);
System.out.println("\nThe comparison result for '" + str2 + "' and '" + str3 + "' is: \n" + result2);
}
}
The explanation of the above code snippet is as follows:
- First, the class “javabeat” is created which contains a “main()” method.
- Inside it, three String type variables “str1”, “str2”, and “str3” are declared and initialized with random values.
- Next, the “toLowerCase()” method is applied to the previously created variables. The result of these methods is stored in the “convertStr1”, “convertStr2”, and “convertStr3” variables.
- The “convertStr1” and “convertStr2” are used with the “.equals()” method to compare both variables. The result is then stored in the boolean type variable named “result1”.
- After that, the “convertStr2” and “convertStr3” variables are compared using the “.equals()” method. The generated result for these variable comparisons is stored in the boolean type variable named “result2”.
- Finally, the “result1” and “result2” variables are displayed on the console.
The generated output shows the comparison result for case-insensitive variables:
Example 3: Using String toLowerCase() Method With Locale Parameter
In this example, the second variant of the “toLowerCase()” method that contains “Locale” as a parameter is being implemented. For instance, a single String having the Locale parameter for “Lithuanian” and “English” is converted into lowercase:
import java.util.Locale;//Importing Required "Locale" Package
class javabeat {
public static void main(String args[]) {
String str1 = "Learn Java With JavaBeat";
Locale Lithuanian = Locale.forLanguageTag("lt");
Locale ENGLISH = Locale.forLanguageTag("en");
String convertStr1 = str1.toLowerCase(Lithuanian);
String convertStr2 = str1.toLowerCase(ENGLISH);
System.out.println("\nThe Lowercase version of '" + Lithuanian + "' Locale for '" + str1 + "' is: \n" + convertStr1);
System.out.println("\nThe Lowercase version of '" + ENGLISH + "' Locale for '" + str1 + "' is: \n" + convertStr2);
}
}
The description of the above code is as follows:
- First, the “Locale” java utility is imported and a class named “javabeat” is created. The String type variable “str1” is then declared and initialized with a dummy value.
- The “forLanguageTag()” method of the “Locale” class having the ISO language code as the parameter is then invoked. The ISO language code in our case is “lt” and “en” for “Lithuanian” and “English” languages, respectively.
- Results of the mentioned method for both Locales are stored in the “convertStr1” and “convertStr2” variables respectively.
- These variables are displayed on the console for verification purposes.
Output for the provided code snippet shows that the Strings are converted into lowercase according to their Locale region:
Internal Implementation of “toLowerCase()” Method in Java
Internal implementation of the “toLowerCase()” method in Java, is shown in the below code snippet:
public String toLowerCase(Locale locData) {
if (locData == null) {
throw new NullPointerException();
}
int upperCaser;
final int StringLength = provVal.length;
scan: {
for (upperCaser = 0 ; upperCaser < StringLength; )
{
char x = provVal[upperCaser];
if ((x >= Character.MIN_HIGH_SURROGATE)&& (x <= Character.MAX_HIGH_SURROGATE)) {
int tempChar = codePointAt(upperCaser);
if (tempChar != Character.toLowerCase(tempChar)) {
break scan;
}
upperCaser += Character.charCount(tempChar);
} else {
if (x != Character.toLowerCase(x)) {
break scan;
}
upperCaser++;
}
}
return this;
}
char[] genOutput = new char[StringLength];
int outputOffset = 0;
System.arraycopy(provVal, 0, genOutput, 0, upperCaser);
String localeLang = locData.getLanguage();
boolean localeProps =(localeLang == "tr" || localeLang == "az" || localeLang == "lt");
char[] lowercaseArray;
int lowercaseChar, stringChar, stringCount;
for (int z = upperCaser; z < StringLength; z += stringCount) {
stringChar = (int)provVal[z];
if ((char)stringChar >= Character.MIN_HIGH_SURROGATE
&& (char)stringChar <= Character.MAX_HIGH_SURROGATE) {
stringChar = codePointAt(z);
stringCount = Character.charCount(stringChar);
} else {
stringCount = 1;
}
if (localeProps || stringChar == '\u03A3') {
//Character Checking for 'Sigma' Letter
lowercaseChar = ConditionalSpecialCasing.toLowerCaseEx(this, z, locData);
} else if (stringChar == '\u0130') {
//Character Checking for 'I DOT' Letter
lowercaseChar = Character.ERROR;
} else {
lowercaseChar = Character.toLowerCase(stringChar);
}
if((lowercaseChar == Character.ERROR)
|| (lowercaseChar >= Character.MIN_SUPPLEMENTARY_CODE_POINT)) {
if (lowercaseChar == Character.ERROR) {
if (!localeProps && stringChar == '\u0130') {
lowercaseArray = ConditionalSpecialCasing.toLowerCaseCharArray(this, z, Locale.ENGLISH);
} else {
lowercaseArray = ConditionalSpecialCasing.toLowerCaseCharArray(this, z, locData);
}
} else if (stringCount == 2) {
outputOffset += Character.toChars(lowercaseChar, genOutput, z + outputOffset) - stringCount;
continue;
} else {
lowercaseArray = Character.toChars(lowercaseChar);
}
/* Enlarging the genOutput according to need */
int bufferSize = lowercaseArray.length;
if (bufferSize > stringCount) {
char[] secondResult = new char[genOutput.length + bufferSize - stringCount];
System.arraycopy(genOutput, 0, secondResult, 0, z + outputOffset);
genOutput = secondResult;
}
for (int y = 0; y < bufferSize; ++y) {
genOutput[z + outputOffset + y] = lowercaseArray[y];
}
outputOffset += (bufferSize - stringCount);
} else {
genOutput[z + outputOffset] = (char)lowercaseChar;
}
}
return new String(genOutput, 0, StringLength + outputOffset);
}
The working procedure for the internal implementation of a “toLowerCase()” method is shown below:
- This method converts the provided String characters into lowercase while considering locale-specific rules.
- The conversion operation is done by iterating through the provided String Characters using “loops”. Then, the case conversion is performed based on the locale region.
- It performs special casing for Locale characters, such as Turkish and Azerbaijan. These are checked for the Greek capital letters “sigma” and the Latin capital letter “I with a dot”.
- After identifying the provided or default locale, the string characters are converted into a lowercase characters array.
- During the conversion process, the Buffer size grows automatically to accept the supplementary characters.
That’s all about the usage and implementation of the String “toLowerCase()” method in Java.
Conclusion
The “toLowerCase()” method is used to convert the cases of all String characters into lowercase. It can be useful at the time of comparing case in-sensitive String or to maintain the coherency by setting the same case for the whole String. The “Locale” region parameter can also be inserted to change the case according to the specified Locale. The toLowerCase() method remains a fundamental tool to achieve consistent and reliable text processing operations like case-insensitive comparisons. This guide has explained the usage and working of the String toLowerCase() method in Java.