I want to convert the first character of a string to Uppercase and the rest of the characters to lowercase. How can I do it?
Example:
String inputval="ABCb" OR "a123BC_DET" or "aBcd"
String outputval="Abcb" or "A123bc_det" or "Abcd"
I want to convert the first character of a string to Uppercase and the rest of the characters to lowercase. How can I do it?
Example:
String inputval="ABCb" OR "a123BC_DET" or "aBcd"
String outputval="Abcb" or "A123bc_det" or "Abcd"
String inputval="ABCb";
String result = inputval.substring(0,1).toUpperCase() + inputval.substring(1).toLowerCase();
Would change "ABCb" to "Abcb"
WordUtils.capitalizeFully(str)
from apache commons-lang has the exact semantics as required.
Try this on for size:
String properCase (String inputVal) {
// Empty strings should be returned as-is.
if (inputVal.length() == 0) return "";
// Strings with only one character uppercased.
if (inputVal.length() == 1) return inputVal.toUpperCase();
// Otherwise uppercase first letter, lowercase the rest.
return inputVal.substring(0,1).toUpperCase()
+ inputVal.substring(1).toLowerCase();
}