Here you can find the source of capitalizeFirstLetter(String str)
capitalizeFirstLetter(null) = null; capitalizeFirstLetter("") = ""; capitalizeFirstLetter("2ab") = "2ab" capitalizeFirstLetter("a") = "A" capitalizeFirstLetter("ab") = "Ab" capitalizeFirstLetter("Abc") = "Abc"
Parameter | Description |
---|---|
str | a parameter |
public static String capitalizeFirstLetter(String str)
//package com.java2s; public class Main { /**/* w w w. j a v a 2 s .co m*/ * capitalize first letter * * <pre> * capitalizeFirstLetter(null) = null; * capitalizeFirstLetter("") = ""; * capitalizeFirstLetter("2ab") = "2ab" * capitalizeFirstLetter("a") = "A" * capitalizeFirstLetter("ab") = "Ab" * capitalizeFirstLetter("Abc") = "Abc" * </pre> * * @param str * @return */ public static String capitalizeFirstLetter(String str) { if (isEmpty(str)) { return str; } char c = str.charAt(0); return (!Character.isLetter(c) || Character.isUpperCase(c)) ? str : new StringBuilder(str.length()) .append(Character.toUpperCase(c)) .append(str.substring(1)).toString(); } /** * is null or its length is 0 * * <pre> * isEmpty(null) = true; * isEmpty("") = true; * isEmpty(" ") = false; * </pre> * * @param str * @return if string is null or its size is 0, return true, else return false. */ public static boolean isEmpty(String str) { return (str == null || str.length() == 0); } }