Java - Write code to capitalize the first letter of a string

Requirements

Write code to capitalize the first letter of a string

abc becomes Abc.

Hint

You can convert the string to char array.

Demo

//package com.book2s;

public class Main {
    public static void main(String[] argv) {
        String str = "book2s.com";
        System.out.println(capitalize(str));
    }/* ww w .  j  ava  2  s .  co m*/

    public static String capitalize(String str) {

        if (str == null || str.isEmpty()) {
            return "";
        }

        char[] buffer = str.toCharArray();
        buffer[0] = Character.toTitleCase(buffer[0]);

        return new String(buffer);
    }
}