Java String Handling

Convert String to uppercase or lowercase in Java Example

This Java example shows how to convert String to uppercase or lowercase using toUpperCase() and toLowerCase() methods of String class. Example also shows how to convert String to upper and lower case using Apache Commons.

How to convert String to uppercase?

To convert String to upper case use toUpperCase() method of Java String class.

This method converts all the characters of a String to upper case using the default locale. Internally it is same as calling toUpperCase(Locale.getDefault()) method call.

Output

Convert String to upper case using Apache Commons Library

If you are using Apache Commons library in your project, you can use upperCase method of StringUtils class to change case of String to upper case.

How to convert String to lowercase?

To convert String to lower case use toLowerCase() method of Java String class.

This method converts all the characters of a String to lower case using the default locale. Internally it is same as calling toLowerCase(Locale.getDefault()) method call.

Output

Convert String to lower case using Apache Commons Library

If you are using Apache Commons library in your project, you can use lowerCase method of StringUtils class to change case of String to lower case.

Important Note:

Both toUpperCase() and toLowerCase() methods are locale sensitive. It could produce unexpected results if used on the strings which are intended to be interpreted locale independently.

If you are working with the string values which are locale insensitive, you should use toUpperCase(Locale locale) or toLowerCase(Locale locale) methods instead.

Please let us know your views in the comments section below.

 

Want to learn quickly?
Try one of the many quizzes. I promise you will not be disappointed.

About the author

rahimv

rahimv

rahimv has over 15 years of experience in designing and developing Java applications. His areas of expertise are J2EE and eCommerce. If you like the website, follow him on Facebook, Twitter or Google Plus.

Add Comment

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