Java String Case Conversion Sample Code

At times there is a need to convert a String to either upper case or lower case. The Java String class has a method to accomplish both situations.

The toLowerCase() method will convert all the characters in a given String to lower case. The method takes no arguments and returns a new String in all lower case.

The toUpperCase() method will convert all the characters in a given String to upper case. The method takes no arguments and returns a new String in all upper case.

Good to note that both of these methods are locale sensitive and may produce an unexpected result if the Strings are locale independent.

Class Name Method Parameters Return Type Exceptions
String toLowerCase() none String none
String toUpperCase() none String none

Java String Case Conversion Source Code:

package com.jcd.java.tutorials;

public class JavaStringCaseConvert 
{

	public static void main(String[] args) 
	{
		
		String myLowerCaseString = "my string in all lower case";
		
		String myUpperCaseString = "MY STRING IN ALL UPPER CASE";
		
		String myMixedCaseString = "My StrING in BOTH upper aND LOWER CasE";
		
		
		System.out.println("myLowerCaseString before: "+myLowerCaseString);
		System.out.println("myLowerCaseString after convert to upper: "+convertUpperCase(myLowerCaseString));
		
		System.out.println("----------------------------------------------------------------------------------");
		
		System.out.println("myUpperCaseString before: "+myUpperCaseString);
		System.out.println("myUpperCaseString after convert to lower: "+convertLowerCase(myUpperCaseString));
		
		System.out.println("----------------------------------------------------------------------------------");
		
		System.out.println("myMixedCaseString before: "+myMixedCaseString);
		System.out.println("myMixedCaseString after convert to upper: "+convertUpperCase(myMixedCaseString));
		
		System.out.println("----------------------------------------------------------------------------------");
		
		System.out.println("myMixedCaseString before: "+myMixedCaseString);
		System.out.println("myMixedCaseString after convert to lower: "+convertLowerCase(myMixedCaseString));
		
	}
	
	//Method to take a string and return a new string in all upper case
	public static String convertUpperCase(String string)
	{	
		
		return string.toUpperCase();
	}
	
	//Method to take a string and return a new string in all lower case
	public static String convertLowerCase(String string)
	{	
		
		return string.toLowerCase();
	}

}


Java String Case Conversion Output:

myLowerCaseString before: my string in all lower case
myLowerCaseString after convert to upper: MY STRING IN ALL LOWER CASE
----------------------------------------------------------------------------------
myUpperCaseString before: MY STRING IN ALL UPPER CASE
myUpperCaseString after convert to lower: my string in all upper case
----------------------------------------------------------------------------------
myMixedCaseString before: My StrING in BOTH upper aND LOWER CasE
myMixedCaseString after convert to upper: MY STRING IN BOTH UPPER AND LOWER CASE
----------------------------------------------------------------------------------
myMixedCaseString before: My StrING in BOTH upper aND LOWER CasE
myMixedCaseString after convert to lower: my string in both upper and lower case



References:

  1. Java String Class

Add a Comment

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