Java String Title Case toTitleCase(String input, boolean eachWord)

Here you can find the source of toTitleCase(String input, boolean eachWord)

Description

to Title Case

License

Open Source License

Parameter

Parameter Description
input the string input
eachWord true to indicate each word should be put into title case

Return

the string capitalized

Declaration

public static String toTitleCase(String input, boolean eachWord) 

Method Source Code

//package com.java2s;

public class Main {
    /**/*from ww w.  j av  a 2  s .  c  o m*/
     * @param input    the string input
     * @param eachWord true to indicate each word should be put into title case
     * @return the string capitalized
     */
    public static String toTitleCase(String input, boolean eachWord) {
        StringBuilder titleCase = new StringBuilder();
        boolean nextTitleCase = true;

        for (char c : input.toCharArray()) {
            if (Character.isSpaceChar(c) && eachWord) {
                nextTitleCase = true;
            } else if (nextTitleCase) {
                c = Character.toTitleCase(c);
                nextTitleCase = false;
            } else {
                c = Character.toLowerCase(c);
            }

            titleCase.append(c);
        }

        return titleCase.toString();
    }
}

Related

  1. toTitleCase(final String inStr, final boolean putRestInLC)
  2. toTitleCase(final String s)
  3. toTitleCase(final String text)
  4. toTitleCase(String givenString)
  5. toTitleCase(String input)
  6. toTitleCase(String inputStr)
  7. toTitleCase(String name)
  8. toTitleCase(String original)
  9. toTitleCase(String original)