How to capitalize the first letter of word in a string using Java?

Simplest way is to use org.apache.commons.lang.StringUtils class

StringUtils.capitalize(Str);


Also, There is org.springframework.util.StringUtils in Spring Framework:

StringUtils.capitalize(str);

If you only want to capitalize the first letter of a string named input and leave the rest alone:

String output = input.substring(0, 1).toUpperCase() + input.substring(1);

Now output will have what you want. Check that your input is at least one character long before using this, otherwise you'll get an exception.


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

Just... a complete solution, I see it kind of just ended up combining what everyone else ended up posting =P.

Tags:

Java