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
- public String toLowerCase()
public String toLowerCase()
- 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
- public class StringLowerExample1{
- public static void main(String args[]){
- String str="JAVAinsimpleWAY";
- String strLower=str.toLowerCase();
- System.out.println(strLower);
- }
- }
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
- 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);
- }
- }
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
- public String toUpperCase()
public String toUpperCase()
- 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
- public class StringUpperExample1{
- public static void main(String args[]){
- String str="JAVAinsimpleWAY";
- String strUpper=str.toUpperCase();
- System.out.println(strUpper);
- }
- }
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
- 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);
- }
- }
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.