Java String Capitalize capitalize(final String str)

Here you can find the source of capitalize(final String str)

Description

 StringUtils.capitalize(null)  = null StringUtils.capitalize("")    = "" StringUtils.capitalize("cat") = "Cat" StringUtils.capitalize("cAt") = "CAt" 

License

Apache License

Parameter

Parameter Description
str the String to capitalize, may be null

Return

the capitalized String, null if null String input

Declaration

public static String capitalize(final String str) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

public class Main {
    /**/*from ww w  .j a  va2 s  .co m*/
     * <pre>
     * StringUtils.capitalize(null)  = null
     * StringUtils.capitalize("")    = ""
     * StringUtils.capitalize("cat") = "Cat"
     * StringUtils.capitalize("cAt") = "CAt"
     * </pre>
     *
     * @param str the String to capitalize, may be null
     * @return the capitalized String, {@code null} if null String input
     * @since 2.0
     */
    public static String capitalize(final String str) {
        int strLen;
        if (str == null || (strLen = str.length()) == 0) {
            return str;
        }

        final char firstChar = str.charAt(0);
        if (Character.isTitleCase(firstChar)) {
            // already capitalized
            return str;
        }

        return new StringBuilder(strLen).append(Character.toTitleCase(firstChar)).append(str.substring(1))
                .toString();
    }
}

Related

  1. capitalize(final String str)
  2. capitalize(final String str)
  3. capitalize(final String str)
  4. capitalize(final String str)
  5. capitalize(final String str)
  6. capitalize(final String str)
  7. capitalize(final String str)
  8. capitalize(final String str)
  9. capitalize(final String str, final char... delimiters)