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" StringUtils.capitalize("'cat'") = "'cat'"

License

Apache License

Parameter

Parameter Description
str a parameter

Declaration

public static String capitalize(final String str) 

Method Source Code

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

public class Main {
    /**//  w  ww.  j a  va 2s.co m
     * StringUtils.capitalize(null)  = null
     * StringUtils.capitalize("")    = ""
     * StringUtils.capitalize("cat") = "Cat"
     * StringUtils.capitalize("cAt") = "CAt"
     * StringUtils.capitalize("'cat'") = "'cat'"
     *
     * @param str
     * @return
     */
    public static String capitalize(final String str) {
        int strLen;
        if (str == null || (strLen = str.length()) == 0) {
            return str;
        }

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

        char[] newChars = new char[strLen];
        newChars[0] = newChar;
        str.getChars(1, strLen, newChars, 1);
        return String.valueOf(newChars);
    }
}

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, final char... delimiters)
  8. capitalize(final String text)
  9. capitalize(final String value)