Menu
Topics Index
...
`

Case Conversion in a string - Java Example Program


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.

© meritcampus 2019

All Rights Reserved.

Open In App