Java String Capitalize Word capitalizeFirstLetterEachWord(String sentence)

Here you can find the source of capitalizeFirstLetterEachWord(String sentence)

Description

Capitalize the first letter of each word in a series of words separated by a space character.

License

Open Source License

Parameter

Parameter Description
sentence The text to capitalize.

Return

A formatted text with each first letter of each word is capitalized.

Declaration

public static String capitalizeFirstLetterEachWord(String sentence) 

Method Source Code

//package com.java2s;
/* /*from   ww w . ja v  a  2  s.  c  om*/
 * The MIT License
 *
 * Copyright 2014 Ryan Gilera ryangilera@gmail.com.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

public class Main {
    /**
     * Capitalize the first letter of each word in a series of words 
     * separated by a space character.
     * @param sentence The text to capitalize.
     * @return A formatted text with each first letter of each word is capitalized.
     */
    public static String capitalizeFirstLetterEachWord(String sentence) {
        if (sentence == null) {
            return null;
        }

        if (sentence.isEmpty()) {
            return sentence;
        }

        sentence = sentence.trim();

        if (sentence.length() == 1) {
            return sentence.toUpperCase();
        }

        // Capitalize first word first
        String firstLetterWordFormatted = capitalizeFirstLetterWord(sentence);

        // Converts all first letter of words separated by space to uppercase
        for (int i = 0; i < sentence.length(); i++) {
            if (sentence.charAt(i) == ' ') {
                firstLetterWordFormatted = firstLetterWordFormatted.substring(0, i + 1)
                        + firstLetterWordFormatted.substring(i + 1, i + 2).toUpperCase()
                        + firstLetterWordFormatted.substring(i + 2);
            }
        }

        // Get back the output string reference
        return firstLetterWordFormatted;
    }

    /**
     * Capitalize only the first letter of a given string.
     * @param word The string text to capitalize.
     * @return A formatted string with the first letter capitalise.
     */
    public static String capitalizeFirstLetterWord(String word) {
        if (word == null) {
            return null;
        }

        if (word.isEmpty()) {
            return word;
        }

        if (word.length() == 1) {
            return word.toUpperCase();
        }

        String newWord = word.trim();

        return newWord.substring(0, 1).toUpperCase() + newWord.substring(1);
    }
}

Related

  1. CapitalizeEachWord(String Str)
  2. capitalizeEachWord(String string)
  3. capitalizeFirstCharWords(String sentence)
  4. capitalizeFirstLetter(final String word)
  5. capitalizeFirstLetter(String word)
  6. capitalizeFirstLetterWord(String word)
  7. capitalizeFirsts(String word)
  8. capitalizeInitials(String words)
  9. capitalizeOneWord(String inputWord)