Java String toLowerCase()

The toLowerCase() method is a member of the java.lang.String class, making it accessible for all Java strings. The toLowerCase() method transforms a String by converting all of its characters to lowercase, using the Locale rules if specified. It does not change the characters that are already in lowercase.

See Also: String toUpperCase() Method

1. String.toLowerCase() Method

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);

To better understand its functionality, let’s examine how it works in practice:

String original = "Hello, World!";

String lowercase = original.toLowerCase();

System.out.println(lowercase); // Output: hello, world!

As demonstrated above, the toLowerCase() method efficiently converts all letters in the string to lowercase.

2. String.toLowerCase() with Custom Locale

The following Java program converts the specified Greek string to lowercase while specifying the Locale as Locale.US.

“Γειά σου Κόσμε” is the original string, which is in Greek and means “Hello, World!” in English. The toLowerCase() converts this string to lowercase using the rules of the specified Locale i.e. Locale.US, indicating that the conversion should follow the rules and conventions of the United States locale.

String originalText = "Γειά σου Κόσμε";

String lowercaseText = originalText.toLowerCase(Locale.US);

System.out.println(lowercaseText);

As you can see, the characters ‘Γ’ and ‘Κ’ are transformed into their lowercase equivalents, ‘γ’ and ‘κ’ respectively. The other characters, such as the Greek diacritics, remain unchanged.

γειά σου κόσμε

3. Usages of toLowerCase() Method

Let us explore some usages of toLowerCase() method in real-world programming.

The toLowerCase() method can be used to ensure uniformity in user-generated content, such as email addresses. We can use toLowerCase() to normalize user inputs.

String userInput = "UsEr@ExAmPle.CoM";

String normalizedInput = userInput.toLowerCase();

System.out.println(normalizedInput); // Output: user@example.com

During data processing, we may require data cleaning due to inconsistent capitalization. The toLowerCase() can help in standardizing the data such as names, URLs, or emails.

String dirtyData = "jOhN dOe,MAry SmiTh,jAnE SmiTh";

String[] names = dirtyData.split(",");

for (String name : names) {
    String cleanName = name.trim().toLowerCase();
    System.out.println(cleanName);
}

4. Length of the Original and Lowercase Strings can be Unequal

Note that the length of the original string and lowercase string may be different due to a few locale-specific characters.

A classic example of this occurs when converting the German character ‘ß’ to lowercase. In German, ‘ß’ can be converted to ‘ss’ when capitalized.

String originalText = "STRAßE"; // German word for "street"

String lowercaseText = originalText.toLowerCase(Locale.GERMAN); // Using the German locale

System.out.println("Original Text: " + originalText);
System.out.println("Lowercase Text: " + lowercaseText);
System.out.println("Original Length: " + originalText.length());
System.out.println("Lowercase Length: " + lowercaseText.length());

In this example, we have a string "STRAßE" which means “street” in German. When we convert it to lowercase using the German locale (Locale.GERMAN), the ‘ß’ character transforms into ‘ss’.

Original Text: STRAßE
Lowercase Text: strasse

Original Length: 6
Lowercase Length: 7

So, when working with the toLowerCase() method in Java and considering locale-specific characters, it’s essential to be aware that the length of the resulting string may not always be the same.

Happy Learning !!

References: String Java Doc

Source Code on Github

Comments

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments

About Us

HowToDoInJava provides tutorials and how-to guides on Java and related technologies.

It also shares the best practices, algorithms & solutions and frequently asked interview questions.

Our Blogs

REST API Tutorial

Dark Mode

Dark Mode