Here you can find the source of capitalize(final String str)
StringUtils.capitalize(null) = null StringUtils.capitalize("") = "" StringUtils.capitalize("cat") = "Cat" StringUtils.capitalize("cAt") = "CAt"
Parameter | Description |
---|---|
str | the String to capitalize, may be null |
public static String capitalize(final String str)
//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(); } }