Java : String Case Conversion


 

Java : String Case Conversion

In this section we will discuss how to convert a String from one case into another case.

In this section we will discuss how to convert a String from one case into another case.

Java : String Case Conversion

In this section we will discuss how to convert a String from one case into another case.

Upper to Lower case Conversion :

By using toLowerCase() method you can convert any upper case char/String into lower case char/String.

Lower to Upper case Conversion :

By using toUpperCase() method you can convert any lower case char/String into upper case char/String.

Example :

class StringCaseConversion {
public static void main(String[] args) {
String uStr = "Hello ROSEINDIA";
String lStr = "hello world";
String uCaseStr, lCaseStr;

// Convert into lower case
lCaseStr = uStr.toLowerCase();
System.out.println("String in lower case: " + lCaseStr);

// Convert into upper case
uCaseStr = lStr.toUpperCase();
System.out.println("String in upper case: " + uCaseStr);
}
}

Description : This example shows conversion of String from one case to another case. 'uStr' String type variable containing upper case string value and 'lStr' containing lower case String value. We have another two String type variable uCaseStr and lCaseStrto for result. Next we are going to convert uStr into lower case by using toLowerCase() method and result is stored in lCaseStr as  - lCaseStr = uStr.toLowerCase();   In the same way we can convert 'lStr' ito upper case by writing code - uCaseStr = lStr.toUpperCase();

Output :

String in lower case: hello roseindia
String in upper case: HELLO WORLD

Ads