capitalize string and handle null value - Android java.lang

Android examples for java.lang:String Case

Description

capitalize string and handle null value

Demo Code

public class Main {

  /**// w w w  . j  a va 2  s .c  om
   * <pre>
   * StringUtil.capitalize(null)  = null
   * StringUtil.capitalize("Han") = "Han"
   * StringUtil.capitalize("han") = "Han"
   * </pre>
   * 
   */
  public static String capitalize(String str) {
    if (str == null) {
      return null;
    }
    char[] charArray = str.toCharArray();
    if (charArray.length > 0) {
      charArray[0] = Character.toUpperCase(charArray[0]);
    }
    return new String(charArray);
  }

}

Related Tutorials