Java - Write code to Capitalizes the first letter of the given string.

Requirements

Write code to Capitalizes the first letter of the given string.

Hint

You do not need to check for the null value.

You can use the substring to get the first letter.

Demo

//package com.book2s;

public class Main {
    public static void main(String[] argv) {
        String text = "book2s.com";
        System.out.println(capitalize(text));
    }// w ww.ja  va 2 s .c om

    /**
     * Capitalizes the first letter of the given string.
     * 
     * @param text the string to capitalize.
     * 
     * @return the modified string.
     */
    public static String capitalize(String text) {
        String h = text.substring(0, 1).toUpperCase();
        String t = text.substring(1);
        return h + t;
    }
}

Related Exercise