Java - Write code to capitalize a String and handle the null input


Write code to capitalize a string

Handle null value, if the null value is passed in just return it back


Use if statement to check of the string is a null value.

Use Character.toUpperCase to convert the first letter to upper case.


//package com.book2s;

public class Main {
    public static void main(String[] argv) {
        String str = "";
    }/* www.  ja v  a 2s  .c  o m*/

    public static String capitalize(String str) {
        if (isEmpty(str))
            return str;
        char[] cs = str.toCharArray();
        cs[0] = Character.toUpperCase(cs[0]);
        return new String(cs);

    public static boolean isEmpty(String str) {
        return str == null || str.length() == 0;