Java String Capitalize Word capitalizeWords(String str)

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

Description

Capitalizes all the whitespace separated words in a String.

License

Apache License

Parameter

Parameter Description
str the String to capitalize, may be null

Return

capitalized String, null if null String input

Declaration

public static String capitalizeWords(String str) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

public class Main {
    /**//from w ww  . j ava2 s.c  om
     * <p>
     * Capitalizes all the whitespace separated words in a String. Only the first letter of each word is changed. To
     * convert the rest of each word to lowercase at the same time, use {@link #capitalize(String)}.
     * </p>
     * 
     * <p>
     * Whitespace is defined by {@link Character#isWhitespace(char)}. A <code>null</code> input String returns
     * <code>null</code>. Capitalization uses the unicode title case, normally equivalent to upper case.
     * </p>
     * 
     * <pre>
     * WordUtils.capitalize(null)        = null
     * WordUtils.capitalize("")          = ""
     * WordUtils.capitalize("i am FINE") = "I Am FINE"
     * </pre>
     * 
     * @param str the String to capitalize, may be null
     * @return capitalized String, <code>null</code> if null String input
     */
    public static String capitalizeWords(String str) {
        return capitalizeWords(str, null);
    }

    /**
     * <p>
     * Capitalizes all the delimiter separated words in a String. Only the first letter of each word is changed.
     * </p>
     * 
     * <p>
     * The delimiters represent a set of characters understood to separate words. The first string character and the
     * first non-delimiter character after a delimiter will be capitalized.
     * </p>
     * 
     * <p>
     * A <code>null</code> input String returns <code>null</code>. Capitalization uses the unicode title case, normally
     * equivalent to upper case.
     * </p>
     * 
     * <pre>
     * WordUtils.capitalize(null, *)            = null
     * WordUtils.capitalize("", *)              = ""
     * WordUtils.capitalize(*, new char[0])     = *
     * WordUtils.capitalize("i am fine", null)  = "I Am Fine"
     * WordUtils.capitalize("i aM.fine", {'.'}) = "I aM.Fine"
     * </pre>
     * 
     * @param str the String to capitalize, may be null
     * @param delimiters set of characters to determine capitalization, null means whitespace
     * @return capitalized String, <code>null</code> if null String input
     */
    public static String capitalizeWords(String str, char[] delimiters) {
        int delimLen = (delimiters == null ? -1 : delimiters.length);
        if ((str == null) || (str.length() == 0) || (delimLen == 0)) {
            return str;
        }
        int strLen = str.length();
        StringBuffer buffer = new StringBuffer(strLen);
        boolean capitalizeNext = true;
        for (int i = 0; i < strLen; i++) {
            char ch = str.charAt(i);

            if (isDelimiter(ch, delimiters)) {
                buffer.append(ch);
                capitalizeNext = true;
            } else if (capitalizeNext) {
                buffer.append(Character.toTitleCase(ch));
                capitalizeNext = false;
            } else {
                buffer.append(ch);
            }
        }
        return buffer.toString();
    }

    /**
     * Is the character a delimiter.
     * 
     * @param ch the character to check
     * @param delimiters the delimiters
     * @return true if it is a delimiter
     */
    private static boolean isDelimiter(char ch, char[] delimiters) {
        if (delimiters == null) {
            return Character.isWhitespace(ch);
        }
        for (int i = 0, isize = delimiters.length; i < isize; i++) {
            if (ch == delimiters[i]) {
                return true;
            }
        }
        return false;
    }
}

Related

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