Java examples for java.lang:String Capitalize
Capitalize a String, changing the first letter to upper case as per Character#toUpperCase(char) .
//package com.java2s; public class Main { public static void main(String[] argv) throws Exception { String str = "java2s.com"; System.out.println(capitalize(str)); }/* w ww. j av a 2 s . c o m*/ /** * Capitalize a <code>String</code>, changing the first letter to upper case as per * {@link Character#toUpperCase(char)}. No other letters are changed. * * @param str * the String to capitalize, may be <code>null</code> * @return the capitalized String, <code>null</code> if null */ public static String capitalize(final String str) { return changeFirstCharacterCase(str, true); } private static String changeFirstCharacterCase(final String str, final boolean capitalize) { if ((str == null) || (str.length() == 0)) return str; final StringBuilder sb = new StringBuilder(str.length()); if (capitalize) { sb.append(Character.toUpperCase(str.charAt(0))); } else { sb.append(Character.toLowerCase(str.charAt(0))); } sb.append(str.substring(1)); return sb.toString(); } }