Tuesday, December 11, 2012

How to convert String from lowercase to uppercase and lowercase in Java

Convert String from uppercase to lowercase in Java
Sometime we need to convert String from lowercase to uppercase or from uppercase to lowercase e.g. before printing or storing into database etc. String class in Java provides some utility method to perform this case conversion. You can use toUpperCase() to convert any lower case String to uppercase and toLowerCase() to convert any uppercase String to lowercase. Key thing to remember while using toUpperCase() and toLowerCase() is that they return a different String rather than modifying same String because String is immutable in Java. So if you use old String, assuming it has been converted into uppercase or lowercase then you may create but, instead just store new String returned by these methods into same variable which is hold to store old String. Once again getting familiar with key classes like java.lang.String is very important for Java programmer as we often need to Split String, replace String or remove white space from String etc. String provides several convenient method to do this without using external library. In this Java tutorial we will also see complete Java program to convert String to lowercase and uppercase in Java.

Java program to convert String to lowercase and Uppercase in Java

How to convert lowercase String to uppercase in JavaHere is my complete code example of performing this conversion. We have used standard toUpperCase() and toLowerCase() method to convert String from lowercase to uppercase and subsequently from uppercase to lowercase in Java.

package test;

/**
 * Java program to convert String from lowercase to uppercase and
 * uppercase to lowercase in Java. We can use toUpperCase() to convert any String
 * to upper case in Java and toLowerCase() method of String class to convert any
 * String to lower case in Java.
 *
 * @author http://java67.blogspot.com
 */

public class StringCaseConverter {
 
 
    public static void main(String args[]) {
     
        //example of converting String to Upper case in Java      
        //input String which contains words in lowercase
        String strInLowerCase = "this string is in lowercase";
     
        System.out.println("String before converting to Uppercase");
        System.out.println("input : " + strInLowerCase);
     
        // converting lowercase String to uppercase in Java
        String strInUpperCase = strInLowerCase.toUpperCase(); //toUpperCase return lowercase of String
     
        System.out.println("String after converting to Uppercase");
        System.out.println("output : " + strInLowerCase);
     
     
        //Now let's see an example of converting String to lower case
        String upper = "THIS IS UPPER CASE STRING";
     
        String lower = upper.toLowerCase(); //toLowerCase() return lowercase of String
     
        System.out.println("input String before converting to LowerCase : " + upper);
        System.out.println("output String after converting to LowerCase : " + lower);
    }    
   
}

Output:
String before converting to Uppercase
input : this string is in lowercase
String after converting to Uppercase
output : this string is in lowercase
input String before converting to LowerCase : THIS IS UPPER CASE STRING
output String after converting to LowerCase : this is upper case string


That's all on how to convert lowercase String to Uppercase in Java. We have also seen example of converting Uppercase string to lowercase using method toLowerCase() in Java.

Other String related articles you may find useful

1 comment:

  1. Please note that there are two versions of the toUpperCase and toLowerCase methods in Java (since version 1.1). The version without parameter uses the default Locale (Locale.getDefault()) to convert the String. This can lead to unexpected behaviour in some cases, as the JavaDoc explains: http://docs.oracle.com/javase/7/docs/api/java/lang/String.html#toLowerCase%28%29

    ReplyDelete

Java67 Headline Animator