Write code to Capitalizes a String changing the first letter to title case as per Character#toTitleCase(char)
capitalize(null) = null capitalize("") = "" capitalize("cat") = "Cat" capitalize("cAt") = "CAt"
Use Character.toTitleCase() method
//package com.book2s; public class Main { public static void main(String[] argv) { String str = "book2s.com"; System.out.println(capitalize(str)); }//from w w w . j a va 2 s . c o m /** * <p> * Capitalizes a String changing the first letter to title case as per * {@link Character#toTitleCase(char)}. No other letters are changed. * </p> * * <pre> * capitalize(null) = null * capitalize("") = "" * capitalize("cat") = "Cat" * capitalize("cAt") = "CAt" * </pre> * * @param str * the String to capitalize, may be null * @return the capitalized String, <code>null</code> if null String input * @see #uncapitalize(String) * @since 2.0 */ public static String capitalize(String str) { if (str == null) { return null; } StringBuilder result = new StringBuilder(str.toString()); if (result.length() > 0) { result.setCharAt(0, Character.toTitleCase(result.charAt(0))); } return result.toString(); } }