Android String Capitalize capitalizeFirstLetter(String str)

Here you can find the source of capitalizeFirstLetter(String str)

Description

capitalize first letter
 capitalizeFirstLetter(null)     =   null; capitalizeFirstLetter("")       =   ""; capitalizeFirstLetter("2ab")    =   "2ab" capitalizeFirstLetter("a")      =   "A" capitalizeFirstLetter("ab")     =   "Ab" capitalizeFirstLetter("Abc")    =   "Abc" 

Parameter

Parameter Description
str a parameter

Declaration

public static String capitalizeFirstLetter(String str) 

Method Source Code

//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(&quot;&quot;) = true;
     * isEmpty(&quot;  &quot;) = 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);
    }
}

Related

  1. capitalize(String s)
  2. capitalize(final String name)
  3. decapitalize(final String name)
  4. capitalize(String string)
  5. decapitalize(String string)
  6. capitalize(String line)
  7. capitalize(String s)