Java String toLowerCase() Method with Examples

Get Job-ready: Java Course with 45+ Real-time Projects! - Learn Java

The toLowerCase() method in Java is used to convert all the characters in a string to lowercase. This method is very useful when we need to standardize the case of a string before performing comparisons or other operations.

The toLowerCase() method is an instance method of the Java String class. When called on a string, it returns a new string with all the characters converted to lowercase.

For example, “Hello World”.toLowerCase() would return “hello world”.

The main purpose of the toLowerCase() method is to convert any uppercase characters in a string to lowercase. This standardization of cases allows strings to be compared or processed without worrying about case sensitivity.

Methods of Java String toLowerCase()

The String class provides two versions of the Java toLowerCase() method:

1) toLowerCase()

This is the simplest usage that converts the string to lowercase using default locale.

2) toLowerCase(Locale loc)

This converts the string to lowercase using rules of the provided Locale.

NOTE: Internally, toLowerCase() uses Character mapping tables of the Unicode standard to convert each character to its lowercase equivalent. The mapping depends on the locale provided.

Syntax

1) public String toLowerCase()
2) public String toLowerCase(Locale loc)

Package view of the method

The toLowerCase() method is defined in the java.lang package:

package java.lang;

public class String {
  public String toLowerCase() {
    // implementation
  }
  
  public String toLowerCase(Locale loc) {
   // implementation 
  }
}

Parameters

The toLowerCase(Locale loc) method takes a Locale parameter that specifies the locale whose rules should be used for lowercasing.

For Example:

String lowerCaseStr = str.toLowerCase(Locale.FRENCH);

This converts str to lowercase based on French locale rules.

Return Value

Both versions of toLowerCase() return a new String object that contains the lowercase version of the original string. The original string remains unchanged.

Example Java toLowerCase()

Example 1:

public class LowercaseStringExample {
    public static void main(String[] args) {
        // 1. Custom input string
        String str = "Hello World";

        // 2. Conversion of the string to lowercase
        String lowerStr = str.toLowerCase();

        // 3. Printing the lowercase string
        System.out.println(lowerStr);
    }
}

Output:
hello world

Example 2:

import java.util.Locale;

public class ToLowerCaseExample {
    public static void main(String[] args) {
        // Create a Locale for the Turkish language, where 'i' has a special uppercase form.
        Locale turkishLocale = new Locale("tr", "TR");
        
        // A string with uppercase characters, including 'I' with a special form in Turkish.
        String text = "İstanbul İzmir";

        // Convert the string to lowercase using the Turkish locale.
        String lowercaseText = text.toLowerCase(turkishLocale);
        
        // Display the original and lowercase strings.
        System.out.println("Original Text: " + text);
        System.out.println("Lowercase Text: " + lowercaseText);
    }
}

Output:
Original Text: İstanbul İzmir
Lowercase Text: i̇stanbul i̇zmir

Conclusion

In conclusion, the toLowerCase() method in Java is a valuable tool for standardizing the case of strings, making it easier to perform comparisons and operations without worrying about case sensitivity. It offers two versions, one for basic conversion and another that considers locale-specific rules.

By using Unicode character mapping tables, it efficiently converts characters to their lowercase counterparts. The original string remains unchanged, and the method returns a new lowercase string. This method is a crucial asset in string manipulation tasks, ensuring consistency and facilitating language-specific transformations, as demonstrated in the provided examples.

Did you know we work 24x7 to provide you best tutorials
Please encourage us - write a review on Google

courses

TechVidvan Team

TechVidvan Team provides high-quality content & courses on AI, ML, Data Science, Data Engineering, Data Analytics, programming, Python, DSA, Android, Flutter, full stack web dev, MERN, and many latest technology.

Leave a Reply

Your email address will not be published. Required fields are marked *