- Jan 19, 2007
Making a String All Uppercase or All Lowercase
String string = "Contains some Upper and some Lower."; String string2 = string.toUpperCase(); String string3 = string.toLowerCase();
These two methods transform a string into all uppercase or all lowercase letters. They both return the transformed result. These methods do not change the original string. The original string remains intact with mixed case.
A practical area in which these methods are useful is when storing information in a database. There might be certain fields that you always want to store as all uppercase or all lowercase. These methods make the conversion a snap.
Case conversion is also useful for processing user logins. The user ID field is normally considered to be a field that’s not case sensitive, whereas the password field is case sensitive. So, when comparing the user ID, you should convert to a known case and then compare to a stored value. Alternatively, you can always use the equalsIgnoreCase() method of the String class, which performs a non case sensitive comparison.