How to convert lowercase String to uppercase in Java? Example

Convert String from uppercase to lowercase in Java
Sometimes we need to convert String from lowercase to uppercase or from uppercase to lowercase e.g. before printing or storing into a 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 the 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 the same variable which is held to store old String.


Once again getting familiar with key classes like java.lang.String is very important for Java programmers as we often need to Split String, replace String or remove white space from String etc. String provides several convenient methods to do this without using an external library. In this Java tutorial, we will also see a complete Java program to convert String to lowercase and uppercase in Java.



Java program to convert String to lowercase and Uppercase in Java


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

You can further check out these best Java online courses to learn more about String and other important classes in Java. 





How to convert a string from lowercase to upper case and vice-versa

Here is our complete Java program to convert a given string from lowercase to upper in Java, you can copy this program and run it in your favorite IDE like Eclipse or IntelliJIDEA or you can run it from the command prompt as well.  


How to convert lowercase String to uppercase in Javapackage 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 the upper case string


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


Other String related articles you may find useful

3 comments:

  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
  2. will you please show a example of below quesiton
    Question:-we enter a string containing both upper case and lower case but how to convert uppercase to lower and lover to upper
    please help

    ReplyDelete
  3. you people are out of you mind

    ReplyDelete

Feel free to comment, ask questions if you have any doubt.