Make Java String All Uppercase or All Lowercase



In this program, we will convert a string to both lowercase and uppercase using toLowerCase() and toUpperCase() methods of Java.

The toUpperCase() method converts all of the characters in this String to upper case using the rules of the default locale.

The toLowerCase() method converts all of the characters in this String to lowercase using the rules of the default locale.

Problem Statement

Write a Java program that converts strings to lowercase and uppercase using the toLowerCase() and toUpperCase() methods ?

Input

SELF LEARNING CENTER
TUTORIALS POINT
This is TutorialsPoint
www.tutorialspoint.com

Output

string value = self learning centre
string value = tutorials point
string value = THIS IS TUTORIALSPOINT
string value = WWW.TUTORIALSPOINT.COM

Steps to convert a string to lowercase and uppercase

Following are the steps to convert a string to lowercase and uppercase ?

  • Import all the necessary classes from the java.lang package.
  • Initialize a string with uppercase letters.
  • Use the toLowerCase() method to convert the string to lowercase.
  • Print the resulting lowercase string.
  • Initialize a string with lowercase letters.
  • Use the toUpperCase() method to convert the string to uppercase.
  • Print the resulting uppercase string.

Java program to convert a string to lowercase and uppercase

Below is the Java program to convert a string to lowercase and uppercase ?

import java.lang.*;

public class StringDemo {
   public static void main(String[] args) {
      // converts all upper case letters in to lower case letters
      String str1 = "SELF LEARNING CENTER";
      System.out.println("string value = " + str1.toLowerCase());

      str1 = "TUTORIALS POINT";
      System.out.println("string value = " + str1.toLowerCase());

      // converts all lower case letters in to upper case letters
      String str2 = "This is TutorialsPoint";

      System.out.println("string value = " + str2.toUpperCase());
      str2 = "www.tutorialspoint.com";
      System.out.println("string value = " + str2.toUpperCase());
   }
}

Output

string value = self learning centre
string value = tutorials point
string value = THIS IS TUTORIALSPOINT
string value = WWW.TUTORIALSPOINT.COM

Code Explanation

In this program, the toLowerCase() method is used to convert uppercase strings like "SELF LEARNING CENTER" and "TUTORIALS POINT" into lowercase. Similarly, the toUpperCase() method is used to convert lowercase strings like "This is TutorialsPoint" and "www.tutorialspoint.com" into uppercase. Both methods follow the default locale's rules for case conversion, and the results are printed after each conversion.

Updated on: 2024-09-20T21:41:11+05:30

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements