Here you can find the source of capitalizeFirstLetter(String str)
Parameter | Description |
---|---|
str | a parameter |
public static String capitalizeFirstLetter(String str)
//package com.java2s; public class Main { /**/*from w w w .j av a 2 s . co m*/ * Capitalize the first letter in a word * * @param str * @return */ public static String capitalizeFirstLetter(String str) { if (str.length() == 0) { return str; } else if (str.length() == 1) { return str.toUpperCase(); } char[] string = str.toLowerCase().toCharArray(); // First letter - capatalize it string[0] = Character.toUpperCase(string[0]); // scan for spaces for (int index = 0; index < string.length; index++) { if (string[index] == ' ' && index != string.length) { // convert chars after found spaces to uppercase string[index + 1] = Character .toUpperCase(string[index + 1]); } } return new String(string); } }