Java String Capitalize Word capitalizeWords(final String text)

Here you can find the source of capitalizeWords(final String text)

Description

Capitalizes the first letter of every "word" in a string.

License

Open Source License

Parameter

Parameter Description
text The text to be capitalized.

Return

The capitalized text.

Declaration

public static final String capitalizeWords(final String text) 

Method Source Code

//package com.java2s;
/*//from w w  w . ja va  2  s  .co m
 * $Header: Util.java, 21/10/2005 23:17:40 luisantonioa Exp $
 *
 * $Author: luisantonioa $
 * $Date: 21/10/2005 23:17:40 $
 * $Revision: 1 $
 * $Log: Util.java,v $
 * Revision 1  21/10/2005 23:17:40  luisantonioa
 * Added copyright notice
 *
 *
* This program is free software: you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation, either version 3 of the License, or (at your option) any later
 * version.
 * 
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
 * details.
 * 
 * You should have received a copy of the GNU General Public License along with
 * this program. If not, see <http://www.gnu.org/licenses/>.
 */

public class Main {
    /**
     * Capitalizes the first letter of every "word" in a string.
     * <p>
     * For example: <br>
     * "hello out there" becomes "Hello Out There"
     * 
     * @param text The text to be capitalized.
     * @return The capitalized text.
     */
    public static final String capitalizeWords(final String text) {
        if (text.isEmpty())
            return text;

        final char[] chars = text.toCharArray();
        chars[0] = Character.toUpperCase(chars[0]);

        final int checkLenth = text.length() - 1;
        for (int i = 0; i < checkLenth;) {
            if (Character.isWhitespace(chars[i++]))
                chars[i] = Character.toUpperCase(chars[i]);
        }

        return new String(chars);
    }
}

Related

  1. capitalizeWord(String s)
  2. capitalizeWord(String str)
  3. capitalizeWord(String word)
  4. capitalizeWord(String word)
  5. capitalizeWord(String word)
  6. capitalizeWords(String data)
  7. capitalizeWords(String s)
  8. capitalizeWords(String str)
  9. capitalizeWords(String str)