toLowerCase() and toUpperCase()

If we want to convert all string characters into lower case or upper case, we can use these methods.

toLowerCase()


This method returns the string with all the characters converted into lower case if any in upper case.

There are 2 overloaded methods in toLowerCase()

One which takes locale and one which does not.

Signature of the methods

  1. public String toLowerCase()
public String toLowerCase()

  1. public String toLowerCase(Locale locale)
public String toLowerCase(Locale locale)


public String toLowerCase()

This method returns a new string by Converting all the characters in this String to lower case (using the default locale)

Example

  1. public class StringLowerExample1{  
  2. public static void main(String args[]){  
  3. String str="JAVAinsimpleWAY";  
  4. String strLower=str.toLowerCase();  
  5. System.out.println(strLower);  
  6. }
  7. }
public class StringLowerExample1{  
public static void main(String args[]){  
String str="JAVAinsimpleWAY";  
String strLower=str.toLowerCase();  
System.out.println(strLower);  
}
}



public String toLowerCase(Locale locale)

This method returns a new string by Converting all the characters in this String to lower case according to the given locale

Example

  1. import java.util.Locale;
  2. public class StringLowerExample2{  
  3. public static void main(String args[]){  
  4. String str="JAVAinsimpleWAY";  
  5. String strLower=str.toLowerCase(Locale.ENGLISH);  
  6. System.out.println(strLower);  
  7. }
  8. }
import java.util.Locale;
public class StringLowerExample2{  
public static void main(String args[]){  
String str="JAVAinsimpleWAY";  
String strLower=str.toLowerCase(Locale.ENGLISH);  
System.out.println(strLower);  
}
}


Note:

Since case mappings in different locales are not always 1:1 char mappings, the resulting String may be a different length than the original String.


toUpperCase()

This method returns the string with all the characters converted into Upper case if any in lower case.

There are 2 overloaded methods in toUpperCase()

One which takes locale and one which does not.

Signature of the methods

  1. public String toUpperCase()
public String toUpperCase()

  1. public String toUpperCase(Locale locale)
public String toUpperCase(Locale locale)


public String toUpperCase ()

This method returns a new string by Converting all the characters in this String to upper case (using the default locale)

Example

  1. public class StringUpperExample1{  
  2. public static void main(String args[]){  
  3. String str="JAVAinsimpleWAY";  
  4. String strUpper=str.toUpperCase();  
  5. System.out.println(strUpper);  
  6. }
  7. }
public class StringUpperExample1{  
public static void main(String args[]){  
String str="JAVAinsimpleWAY";  
String strUpper=str.toUpperCase();  
System.out.println(strUpper);  
}
}



public String toUpperCase(Locale locale)

This method returns a new string by Converting all the characters in this String to upper case according to the given locale

Example

  1. import java.util.Locale;
  2. public class StringUpperExample2{
  3. public static void main(String args[]){  
  4. String str="JAVAinsimpleWAY";  
  5. String strUpper=str.toUpperCase (Locale.ENGLISH);  
  6. System.out.println(strUpper);  
  7. }
  8. }
import java.util.Locale;
public class StringUpperExample2{ 
public static void main(String args[]){  
String str="JAVAinsimpleWAY";  
String strUpper=str.toUpperCase (Locale.ENGLISH);  
System.out.println(strUpper);  
}
}


Note:

Since case mappings in different locales are not always 1:1 char mappings, the resulting String may be a different length than the original String.

About the Author

Founder of javainsimpleway.com
I love Java and open source technologies and very much passionate about software development.
I like to share my knowledge with others especially on technology 🙂
I have given all the examples as simple as possible to understand for the beginners.
All the code posted on my blog is developed,compiled and tested in my development environment.
If you find any mistakes or bugs, Please drop an email to kb.knowledge.sharing@gmail.com

Connect with me on Facebook for more updates

Share this article on