Java String to LowerCase

Filed Under: Java

Java String to lowercase conversion can be done using toLowerCase() method.

Java String to LowerCase

java string to lowercase

  • Java String toLowerCase() method has two variants – toLowerCase() and toLowerCase(Locale locale).
  • Conversion of the characters to lower case is done by using the rules of default locale. Calling toLowerCase() function is same as calling toLowerCase(Locale.getDefault()).
  • Java String to lower case method is locale sensitive, so use it carefully with strings that are intended to be used locale independently. For example programming language identifiers, HTML tags, protocol keys etc. Otherwise you might get unwanted results.
  • To get correct lower case results for locale insensitive strings, use toLowerCase(Locale.ROOT) method.
  • String toLowerCase() returns a new string, so you will have to assign that to another string. The original string remains unchanged because Strings are immutable.
  • If locale is passed as null to toLowerCase(Locale locale) method, then it will throw NullPointerException.

Java String to LowerCase Example

Let’s see a simple example to convert a string to lower case and print it.


String str = "Hello World!";
System.out.println(str.toLowerCase()); //prints "hello world!"

We can also use Scanner class to get user input and then convert it to lower case and print it.

Here is a complete example program to convert java string to lowercase and print it.


package com.journaldev.string;

import java.util.Scanner;

public class JavaStringToLowerCase {

	public static void main(String[] args) {
		String str = "JournalDev";

		String strLowerCase = str.toLowerCase();

		System.out.println("Java String to Lower Case Example Output: " + strLowerCase);

		readUserInputAndPrintInLowerCase();
	}

	private static void readUserInputAndPrintInLowerCase() {
		Scanner sc = new Scanner(System.in);
		System.out.println("Please provide input String and press Enter:");
		String str = sc.nextLine();
		System.out.println("Input String in Lower Case = " + str.toLowerCase());
		sc.close();
	}

}

Below image shows the output from a sample execution of above program.

Java String toLowerCase example

That’s all for java string to lowercase conversion example.

Reference: API Doc

Leave a Reply

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

close
Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages