Java String Title Case toTitleCase(final String input)

Here you can find the source of toTitleCase(final String input)

Description

Capitalizes all words in a string.

License

Creative Commons License

Parameter

Parameter Description
input string to edit

Exception

Parameter Description
IllegalArgumentException if Input is null or empty

Return

string with all words capitalized

Declaration

public static String toTitleCase(final String input) 

Method Source Code

//package com.java2s;
//License from project: Creative Commons License 

public class Main {
    /**/*from w  w w .  jav  a  2s  .  com*/
     * Capitalizes all words in a string.
     * @param input string to edit
     * @return string with all words capitalized
     * @throws IllegalArgumentException if Input is null or empty
     */
    public static String toTitleCase(final String input) {
        if (input == null || input.isEmpty())
            throw new IllegalArgumentException("Input cannot be null or empty!");
        final String[] words = input.trim().split(" ");
        final StringBuilder sb = new StringBuilder();

        for (final String word : words) {
            sb.append(Character.toUpperCase(word.charAt(0))).append(word.substring(1)).append(' ');
        }
        return sb.toString().trim();
    }
}

Related

  1. titlecase(String str)
  2. titleCase(String txt)
  3. titleCasedName(T instance)
  4. titleCaseTruncate(String s, int maxlen)
  5. toTitleCase(final int chr)
  6. toTitleCase(final String inStr)
  7. toTitleCase(final String inStr, final boolean putRestInLC)
  8. toTitleCase(final String s)
  9. toTitleCase(final String text)