Java String Capitalize Word capitalizeWords(String s)

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

Description

capitalizeWords.

License

Open Source License

Parameter

Parameter Description
s s

Return

String

Declaration

public static String capitalizeWords(String s) 

Method Source Code

//package com.java2s;

public class Main {
    /**//from   ww  w.j a v  a2s.c  om
     * capitalizeWords.
     * @param s s
     * @return String
     */
    public static String capitalizeWords(String s) {
        if (s == null) {
            return null;
        }
        String[] tokens = s.split(" ");
        StringBuilder buf = new StringBuilder();
        for (String x : tokens) {
            buf.append(capitalize(x) + " ");
        }

        return buf.substring(0, buf.length() - 1);
    }

    /**
     * capitalize.
     * @param s String
     * @return String
     */
    public static String capitalize(String s) {
        if (isEmpty(s)) {
            return s;
        }
        return Character.toTitleCase(s.charAt(0)) + s.substring(1);
    }

    /**
     * whether or not the string is null or consists only of whitespace. 
     * @param s 
     * @return boolean
     * */
    public static boolean isEmpty(String s) {
        return s == null || s.trim().length() == 0;
    }
}

Related

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