Java String Capitalize Word capitalizeWords(String str)

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

Description

(Based on ucwords() function of PHP)
DrHouse: still functional but must be rewritten to avoid += to concat strings

License

Open Source License

Parameter

Parameter Description
str - the string to capitalize

Return

a string with the first letter of every word in str capitalized

Declaration

@Deprecated
public static String capitalizeWords(String str) 

Method Source Code

//package com.java2s;
/*//  w  w w . j  a  v  a2  s  . c om
 * This file is part of the L2J Olivia project.
 * 
 * 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 {
    /**
     * (Based on ucwords() function of PHP)<br>
     * DrHouse: still functional but must be rewritten to avoid += to concat strings
     * @param str - the string to capitalize
     * @return a string with the first letter of every word in {@code str} capitalized
     */
    @Deprecated
    public static String capitalizeWords(String str) {
        if ((str == null) || str.isEmpty()) {
            return str;
        }

        final char[] charArray = str.toCharArray();
        final StringBuilder result = new StringBuilder();

        // Capitalize the first letter in the given string!
        charArray[0] = Character.toUpperCase(charArray[0]);

        for (int i = 0; i < charArray.length; i++) {
            if (Character.isWhitespace(charArray[i])) {
                charArray[i + 1] = Character.toUpperCase(charArray[i + 1]);
            }

            result.append(charArray[i]);
        }

        return result.toString();
    }
}

Related

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