The method toLowerCase()
converts the characters of a String
into lower case characters. It has two variants:
String toLowerCase(Locale locale)
: It converts the string into Lowercase using the rules defined by specified Locale.
String toLowerCase()
: It is equivalent to toLowerCase(Locale.getDefault())
. Locale.getDefault()
gets the current value of the default locale for this instance of the Java Virtual Machine. The Java Virtual Machine sets the default locale during startup based on the host environment. It is used by many locale-sensitive methods if no locale is explicitly specified. It can be changed using the setDefault()
method.
Example: toLowerCase() method
import java.util.Locale; public class LowerCaseExample{ public static void main(String args[]){ String str = new String("ABC IS NOT EQUAL TO XYZ"); //Standard method of conversion System.out.println(str.toLowerCase()); //By specifying Locale System.out.println(str.toLowerCase(Locale.FRANCE)); } }
Output:
abc is not equal to xyz abc is not equal to xyz
Method: toUpperCase()
Like toLowerCase()
method, toUpperCase()
also has two variants:
String toUpperCase(Locale locale)
: It converts the string into a UpperCase string using the rules defined by the specified Locale.
String toUpperCase()
: It is equavalent to toUpperCase(Locale.getDefault())
.
Example: toUpperCase() method
import java.util.Locale; public class UpperCaseExample{ public static void main(String args[]){ String str = new String("this is a test string"); //Standard method of conversion System.out.println(str.toUpperCase()); //By specifying Locale System.out.println(str.toUpperCase(Locale.CHINA)); } }
Output:
THIS IS A TEST STRING THIS IS A TEST STRING
Leave a Reply