Question

Example strings...

bob
mARy
AaRoN
JeREMY

Output...

Bob
Mary
Aaron
Jeremy

I've looked around the internet and found the following code...

public String capitalizeFirstLetter(String original){
    if(original.length() == 0)
        return original;
    return original.substring(0, 1).toUpperCase() + original.substring(1);

I know this only capitalizes the first letter of the string, but could I do something like

return original.substring(0, 1).toUpperCase() + original.toLowerCase(1,substring.length);

I'm getting caught up on how to make the rest of the letters in the word lower case. Thanks for your time.

Was it helpful?

Solution

Try this instead:

if (original == null || original.isEmpty())
    return original;
String modified = original.toLowerCase();
return Character.toUpperCase(modified.charAt(0)) + modified.substring(1);

OTHER TIPS

return original.substring(0, 1).toUpperCase() + original.substring(1).toLowerCase();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top