toUpperCase(), toLowerCase() and replace() String

February 21, 2011 | String and StringBuffer
Here, we do some miscellaneous operations on strings like converting a string from lowercase to uppercase or vice versa, removing unwanted prefix and suffix whitespaces and replacing a few characters with replace() String.

Supported methods from String class.

  1. String toLowerCase(): Returns a new string containing all lowercase characters. All the uppercase characters are converted into lowercase. But the original string is not affected (remember, strings are immutable).
  2. String toUpperCase(): Returns a new string containing all uppercase characters. All the lowercase characters are converted into uppercase. But the original string is not disturbed.
  3. String replace(char ch1, char ch2): Replaces all the occurrences of character ch1 with character ch2. Returns a new string, but the original string is intact.
  4. String replace(String str1, String str2): Replaces all the occurrences of string str1 with string str2. Returns a new string, but the original string is undisturbed.
The above methods usage is illustrated in the following program including replace() String.
public class MiscMethods
  public static void main(String args[])
    String s1 = "hyderabad";
    String s2 = "DELHI";

    String s3 = s1.toUpperCase(); 
    System.out.println(s3);  				// prints HYDERABAD
    System.out.println(s1);                             // hyderabad, original string unaffected
    System.out.println(s2.toLowerCase());  		// prints delhi

    String s4 = " pen ";                                // notice, one space before and after pen
    String s5 = s4.trim();
    System.out.println(s4.length());   			// prints 5
    System.out.println(s5.length());  			// prints 3
				                        // replacing a single character
    String s6 = "rajarao";
    String s7 = s6.replace('r','R');
    System.out.println(s7);  				// prints RajaRao
    System.out.println(s6);                             // prints rajarao, original undisturbed
				                        // replacing a string
    String s8 = "aaBBccBB";
    String s9 = s8.replace("BB","bbb");
    System.out.println(s9);  				// prints aabbbccbbb

[caption id="attachment_516" align="aligncenter" width="479" caption="replace() String etc."]replace() String[/caption]

String s3 = s1.toUpperCase();

All the lowercase characters of s1 are converted to uppercase and with the new values, a new string s3 is created and returned. Remember, the original string is not disturbed with the methods toUpperCase(), toLowerCase() and replace() as strings are designed to be immutable.

String s5 = s4.trim();

The prefix and suffix whitespace of s4 is deleted. After deletion, a new string s5 is created and returned.

String s7 = s6.replace('r','R');

All the r characters of s6 are replaced with R and a new string s7 is created and returned.

String s9 = s8.replace("BB","bbb");

All the "BB" characters of s8 are replaced with "bbb" and a new string s9 is created and returned. Two characters are replaced by three characters.