Menu
Topics Index
...
`


Strings >
Siva Nookala - 05 Apr 2016
The case of characters in a String can be changed using toLowerCase() or toUpperCase() method.
  • toLowerCase method changes all the characters in the String to lower case.
  • toUpperCase method changes all the characters in the String to upper case.

Syntax of toLowerCase method is :
public String toLowerCase()
or
public String toLowerCase(Locale locale)



Syntax of toUpperCase method is :
public String toUpperCase()
or
public String toUpperCase(Locale locale)

  • The first version of these methods use a default Locale for case conversion i.e. it is executed internally as toLowerCase(Locale.getDefault()).
  • The second version of these methods are used for internationalization purpose, it performs case conversion with respect to the specified Locale.
Here is a sample program implementing these methods :
Case Conversion in a string
import java.util.Locale;

class CaseConvertDemo
{
    public static void main(String arg[])
    {
        String str = "MeriT CampuS".toUpperCase(); // LINE A
        System.out.println(str);
        System.out.println(str.toLowerCase()); // LINE B
        str = "turkish";
        Locale locale = new Locale("tr"); // LINE C
        System.out.println("String in Turkish locale is: " + str.toUpperCase(locale)); // LINE D
        // System.out.println("String in default locale (English) :" + str.toUpperCase()); // LINE E
        str = "TURKISH";
        System.out.println("String in Turkish locale is: " + str.toLowerCase(locale)); // LINE F
        // System.out.println("String in default locale (English) :" + str.toLowerCase()); // LINE G    
    }
}
OUTPUT

MERIT CAMPUS
merit campus
String in Turkish locale is: TURKİSH
String in Turkish locale is: turkısh

DESCRIPTION

At LINE A, string "MeriT CampuS" is converted to uppercase resulting in "MERIT CAMPUS".
At LINE B, str is converted to lowercase resulting in "merit campus".
A new Locale object is created at LINE C. This Locale object represents TURKEY country and TURKISH language.
Using this Locale "turkish" is converted to uppercase at LINE D, outputting TURKİSH.
Using this Locale "TURKISH" is converted to lowercase at LINE F, outputting turkısh.

THINGS TO TRY
  • Comment LINE D and uncomment LINE E. Here conversion is done using default Locale i.e. English. The conversion results in TURKISH instead of TURKİSH.
  • Comment LINE F and uncomment LINE G. Here conversion is done using default Locale i.e. English. The conversion results in turkish instead of turkısh.
  • Try converting the strings using different Locales and notice the difference.

0
Wrong
Score more than 2 points

© meritcampus 2019

All Rights Reserved.

Open In App