Using toLowerCase( ) and toUpperCase( ) in Java

By: Mashoud Viewed: 161 times  Printer Friendly Format    


The method toLowerCase( ) converts all the characters in a string from uppercase to lowercase. The toUpperCase( ) method converts all the characters in a string from lowercase to uppercase. Nonalphabetical characters, such as digits, are unaffected. Here are the general forms of these methods:

String toLowerCase( )
String toUpperCase( )

Both methods return a String object that contains the uppercase or lowercase equivalent of the invoking String.

Here is an example that uses toLowerCase( ) and toUpperCase( ):

// Demonstrate toUpperCase() and toLowerCase().
class ChangeCase {
public static void main(String args[])
{
String s = "This is a test.";
System.out.println("Original: " + s);
String upper = s.toUpperCase();
String lower = s.toLowerCase();
System.out.println("Uppercase: " + upper);
System.out.println("Lowercase: " + lower);
}
}

The output produced by the program is shown here:

Original: This is a test.
Uppercase: THIS IS A TEST.
Lowercase: this is a test.



Most Viewed Articles (in Java )

Latest Articles (in Java)

Comment on this tutorial