Home | | Web Programming | Changing the Case of Characters Within a String - Java

Chapter: Java The Complete Reference : The Java Library : String Handling

Changing the Case of Characters Within a String - Java

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.

Changing the Case of Characters Within a String

 

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 simplest 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. The default locale governs the conversion in both cases.

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();

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.

 

One other point: Overloaded versions of toLowerCase( ) and toUpperCase( ) that let you specify a Locale object to govern the conversion are also supplied. Specifying the locale can be quite important in some cases and can help internationalize your application.

 

Study Material, Lecturing Notes, Assignment, Reference, Wiki description explanation, brief detail
Java The Complete Reference : The Java Library : String Handling : Changing the Case of Characters Within a String - Java |


Privacy Policy, Terms and Conditions, DMCA Policy and Compliant

Copyright © 2018-2024 BrainKart.com; All Rights Reserved. Developed by Therithal info, Chennai.