Convert String Array to Lowercase Java

Convert String Array to Lowercase Java | Here we will convert an array of strings to lower case using the method available in the Java library. Let us see how to make a string array lowercase Java. Also see:- String Array to UpperCase Java

Example to convert string array to lowercase Java:-
String array = {“Java”, “Python”, “JavaScript”}
The resultant string array after conversion:- {“java”,” python”, “javascript”}

In the java.util.lang package in the string class as this package is default there is no need to import explicitly. The toLowerCase() method is the method that converts the array of strings into the lower case. Similar to the toUpperCase() method toLowerCase() also returns the string which is in lower case.  To understand more let us go through some examples.

// demonstration of toLowerCase() method
String str = "Welcome To Java Program";
String lower = str.toLowerCase();
System.out.println(lower);

The resultant after conversion:- welcome to the java program

There are two variations in the toLowerCase() method
1) public String toLowerCase()
2) public String toLowerCase(locale loc)

But to solve our problem we use the first method which is public string toLowerCase(). 

Convert String Array to Lowercase Java using toLowerCase()

import java.util.Arrays;
import java.util.Scanner;

public class Main {
   public static void main(String args[]) {
      Scanner scan = new Scanner(System.in);

      System.out.print("Enter the number of array elements: ");
      int n = scan.nextInt();
      scan.nextLine(); // clear

      System.out.println("Enter " + n + " strings: ");
      String array[] = new String[n];
      for (int i = 0; i < n; i++) {
         array[i] = scan.nextLine();
      }
      System.out.println("Entered string array: " 
                         + Arrays.toString(array));

      // convert to uppercase
      for (int i = 0; i < n; i++) {
         array[i] = array[i].toLowerCase();
      }
      System.out.println("String array after converting" 
            + " to lowercase: " + Arrays.toString(array));
      scan.close();
   }
}

Output:-

Enter the number of array elements: 3
Enter 3 strings:
HTML
PHP
CSS
Entered string array: [HTML, PHP, CSS]
String array after converting to lowercase: [html, php, css]

Enter the number of array elements: 5
Enter 5 strings:
Java
Spring
JavaScript
Design Pattern
Hibernate
Entered string array: [Java, Spring, JavaScript, Design Pattern, Hibernate]
String array after converting to lowercase: [java, spring, javascript, design pattern, hibernate]

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

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