Java String Capitalize First capitalizeFirstLetter(String str)

Here you can find the source of capitalizeFirstLetter(String str)

Description

Capitalize the first letter in a word

License

Open Source License

Parameter

Parameter Description
str a parameter

Declaration

public static String capitalizeFirstLetter(String str) 

Method Source Code

//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);
    }
}

Related

  1. capitalizeFirstLetter(String s)
  2. capitalizeFirstLetter(String s)
  3. capitalizeFirstLetter(String s)
  4. capitalizeFirstLetter(String s)
  5. capitalizeFirstLetter(String str)
  6. capitalizeFirstLetter(String str)
  7. capitalizeFirstLetter(String str)
  8. capitalizeFirstLetter(String str)
  9. capitalizeFirstLetter(String str)