How To Convert String To Lowercase In Java

A string can be defined as a sequence of characters that are used to represent text data. The characters can either be in the form of  letters, numbers, symbols or any other characters that can be typed on a keyboard. Lowercase refers to the letters that are written in small letters rather than capital letters.

When we speak about converting a string data type to lowercase, we actually mean changing all the capital letters in the string to their corresponding lowercase letters. This is a common operation in programming that can be useful for data processing and manipulation.

Why do we need to convert string to lowercase in java

Some reasons why we need to convert a string to lowercase in Java:

  • String comparisons: When we would like to ensure if any string is correctly inputted, we frequently need to convert both strings to the same case (either uppercase or lowercase).
  • User input: When receiving user input, it is best practice to transform it to lowercase to guarantee consistency and eliminate problems caused by case variations.
  • Formatting: We may need to prepare a string in some circumstances to follow a specified format or style guide. If the style guide wants all text to be lowercase, we may use the toLowerCase() function to format the string correctly.

How to convert string to lowercase in java

Some of the approaches for converting string to lowercase are:

  • Using the toLowerCase() method
  • Using the String.toLowerCase(Locale) method
  • Using the Apache Commons StringUtils class

Approach 1: Using the toLowerCase() method

The toLowerCase() function in Java is used to transform a string to its respective lowercase form. The String class method returns a new string with all capital letters in the original string changed to lowercase counterparts. The toLowerCase() function does not change the original text; instead, it returns a new string with the same characters in lowercase.

public class StringToLowercaseExample {
    public static void main(String[] args) {
        // Define the original string
        String originalString = "Hello World!";
        
        // Call the toLowerCase() method to convert the string to lowercase
        String lowercaseString = originalString.toLowerCase();
        
        // Print the original and lowercase strings to the console
        System.out.println("Original string: " + originalString);
        System.out.println("Lowercase string: " + lowercaseString);
    }
}

Output:

Original string: Hello World!
Lowercase string: hello world!

Explanation:

  • We define a string variable called originalString and assign it the value “Hello World!”.
  • We call the toLowerCase() method on originalString to convert it to lowercase, and store the result in a new string variable called lowercaseString.
  • We print both the original and lowercase strings to the console using System.out.println().
  • The output shows that the original string is unchanged, while the lowercase string has all its characters in lowercase form.

Approach 2: Using the String.toLowerCase(Locale) method

Another approach includes a String.toLowerCase(Locale) method for converting a given string to its lowercase version based on the supplied locale. This function returns a new string with all capital letters in the original string which is then changed to lowercase counterparts according to the locale’s requirements.

import java.util.Locale;

public class StringToLowercaseLocaleExample {
    public static void main(String[] args) {
        // Define the original string
        String originalString = "Über Java";
        
        // Define the locale to use for conversion
        Locale germanLocale = new Locale("de", "DE");
        
        // Call the toLowerCase(Locale) method to convert the string to lowercase based on the specified locale
        String lowercaseString = originalString.toLowerCase(germanLocale);
        
        // Print the original and lowercase strings to the console
        System.out.println("Original string: " + originalString);
        System.out.println("Lowercase string: " + lowercaseString);
    }
}

Output:

Original string: UBer Java
Lowercase string: uber java

Explanation:

  • We define a string variable called originalString and assign it the value “Über Java”. This string includes an umlaut, which is a German special character.
  • We define a Locale object called germanLocale with the language code “de” and the country code “DE”, indicating that we want to convert the string to lowercase based on German cultural conventions.
  • We call the toLowerCase(Locale) method on originalString, passing in germanLocale as the argument, to convert it to lowercase based on German conventions. We store the result in a new string variable called lowercaseString.
  • We print both the original and lowercase strings to the console using System.out.println().
  • The output shows that the original string includes an umlaut character, which is converted to lowercase according to German conventions in the lowercase string.

Approach 3: Using the Apache Commons StringUtils class

The Apache Commons StringUtils class is a utility class that provides a variety of string manipulation methods, including a method in Java for converting a string to lowercase. This class is a component of the Apache Commons Lang library, a widely used third-party library in Java applications.

[Note: You can download the Apache Commons Lang library, which includes the StringUtils class, from the official Apache Commons website at https://commons.apache.org/proper/commons-lang/.

Once you’ve downloaded the library, you can extract the JAR file and add it to your Java project’s classpath to be able to use the StringUtils class in your code.]

import org.apache.commons.lang3.StringUtils;

public class StringUtilsExample {
    public static void main(String[] args) {
        // Define the original string
        String originalString = "HELLO WORLD!";
        
        // Call the StringUtils.lowerCase(String) method to convert the string to lowercase
        String lowercaseString = StringUtils.lowerCase(originalString);
        
        // Print the original and lowercase strings to the console
        System.out.println("Original string: " + originalString);
        System.out.println("Lowercase string: " + lowercaseString);
    }
}

Output:

Original string: Hello WORLD!
Lowercase string: hello world!

Explanation:

  • We define a string variable called originalString and assign it the value “Hello WORLD!”.
  • We import the StringUtils class from Apache Commons, which provides a lowerCase(String) method to convert a string to lowercase.
  • We call the lowerCase(String) method from StringUtils, passing in originalString as the argument, to convert it to lowercase. We store the result in a new string variable called lowercaseString.
  • We print both the original and lowercase strings to the console using System.out.println().
  • The output shows that the original string has both uppercase and lowercase characters, while the lowercase string has all its characters in lowercase form.

Best Approach

Using the .toLowerCase() method for converting string to lowercase in java is best approach because:

  • Simplicity: This method is very easy to use and requires only a single method call to convert a string to lowercase.
  • Performance: This method is highly optimized and runs very quickly, even for long strings.
  • Platform-independent: This method works consistently across all platforms and Java versions, ensuring that your code will behave consistently regardless of where it’s running.
  • Memory-efficient: This method creates a new string object with all lowercase characters, leaving the original string object unchanged. This can be more memory-efficient than creating a new object for each lowercase character.

Sample Problem

Sample Problem 1:

Given a string destination representing a travel destination, write a Java program that converts the string to lowercase and checks if it contains the word “beach”. If it does, print “Pack your sunscreen!”. Otherwise, print “Don’t forget your umbrella!”.

Solution:

  • We start by defining the TravelDestination class and the main method.
  • We are given a travel destination in the form of a string, which we store in the destination variable.
  • To ensure that our program is case-insensitive, we convert the destination string to lowercase using the toLowerCase() method and store it in the lowercaseDestination variable.
  • We then use the contains() method to check if the lowercaseDestination string contains the word “beach”.
  • If it does, we print “Pack your sunscreen!” to suggest that the travel destination is a beach and sunscreen is a must-have item. Otherwise, we print “Don’t forget your umbrella!” to suggest that the destination is not a beach and an umbrella may come in handy.
public class TravelDestination {
  public static void main(String[] args) {
    // Given travel destination
    String destination = "Bali Beach";

    // Convert the string to lowercase
    String lowercaseDestination = destination.toLowerCase();
    System.out.println("Destination: "+lowercaseDestination);

    // Check if the lowercase string contains the word "beach"
    if (lowercaseDestination.contains("beach")) {
      System.out.println("Pack your sunscreen!");
    } else {
      System.out.println("Don't forget your umbrella!");
    }
  }
}

Output:

Destination: bali beach
Pack your sunscreen!

Sample Problem 2:

Given a string movieTitle representing the title of a movie, write a Java program that converts the string to lowercase using the Locale.ENGLISH locale and checks if it contains the word “action”. If it does, print “This is an action movie”. Otherwise, print “This is not an action movie”.

Solution:

  • We define the MovieTitle class and the main method.
  • We are given a movie title in the form of a string, which we store in the movieTitle variable.
  • We convert the movieTitle string to lowercase using the toLowerCase(Locale.ENGLISH) method with the Locale.ENGLISH parameter to ensure consistent behavior across different languages and store it in the lowercaseTitle variable.
  • We then use the contains() method to check if the lowercaseTitle string contains the word “action”.
  • If it does, we print “This is an action movie” to suggest that the movie is an action movie. Otherwise, we print “This is not an action movie” to suggest that the movie is not an action movie.
import java.util.Locale;

public class MovieTitle {
  public static void main(String[] args) {
    // Given movie title
    String movieTitle = "The Matrix: Reloaded";

    // Convert the string to lowercase using Locale.ENGLISH
    String lowercaseTitle = movieTitle.toLowerCase(Locale.ENGLISH);
    System.out.println("Movie Title: "+ lowercaseTitle);

    // Check if the lowercase string contains the word "action"
    if (lowercaseTitle.contains("action")) {
      System.out.println("This is an action movie");
    } else {
      System.out.println("This is not an action movie");
    }
  }
}

Output:

Movie Title: the matrix: reloaded
This is not an action movie

Sample Problem 3:

Given a string bookTitle representing the title of a book, write a Java program that uses the StringUtils class from Apache Commons to convert the string to lowercase and checks if it contains the word “thriller”. If it does, print “This is a thriller book”. Otherwise, print “This is not a thriller book”.

Solution:

  • We start by importing the StringUtils class from Apache Commons and defining the BookTitle class and the main method.
  • We are given a book title in the form of a string, which we store in the bookTitle variable.
  • To ensure that our program is case-insensitive, we convert the bookTitle string to lowercase using the lowerCase() method from the StringUtils class and store it in the lowercaseTitle variable.
  • We then use the contains() method from the StringUtils class to check if the lowercaseTitle string contains the word “thriller”.
  • If it does, we print “This is a thriller book” to suggest that the book is a thriller. Otherwise, we print “This is not a thriller book” to suggest that the book is not a thriller.
import org.apache.commons.lang3.StringUtils;

public class BookTitle {
  public static void main(String[] args) {
    // Given book title
    String C = "The Da Vinci Code: A Thriller";

    // Convert the string to lowercase using StringUtils class
    String lowercaseTitle = StringUtils.lowerCase(bookTitle);
    System.out.println("Book Title: "+ lowercaseTitle);

    // Check if the lowercase string contains the word "thriller"
    if (StringUtils.contains(lowercaseTitle, "thriller")) {
      System.out.println("This is a thriller book");
    } else {
      System.out.println("This is not a thriller book");
    }
  }
}

Output:

Book Title: the da vinci code: a thriller
This is a thriller book

Conclusion

We can conclude by saying that when converting a string to lowercase in java we may accomplish it using the toLowerCase() method of the String class, the toLowerCase(Locale) method of the String class, or the StringUtils class of the Apache Commons Lang library.

It is helpful for assuring case-insensitive string comparisons, standardizing user input, and formatting strings to correspond to a certain style guide.