Java String Capitalize capitalizeString(String text, boolean replaceUnderscore)

Here you can find the source of capitalizeString(String text, boolean replaceUnderscore)

Description

Fix the given text with making the first letter captializsed and the rest not.

License

Open Source License

Parameter

Parameter Description
text the text fixing.
replaceUnderscore True to replace all _ with a space, false otherwise.

Return

The new fixed text.

Declaration

public static String capitalizeString(String text, boolean replaceUnderscore) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

public class Main {
    /**/*  www. java 2 s  . c  om*/
     * Fix the given text with making the first letter captializsed and the rest not.
     * 
     * @param text the text fixing.
     * @param replaceUnderscore True to replace all _ with a space, false otherwise.
     * @return The new fixed text.
     */
    public static String capitalizeString(String text, boolean replaceUnderscore) {
        if (text.isEmpty()) {
            return text;
        }

        if (text.length() == 1) {
            return text.toUpperCase();
        }

        String toReturn = text.substring(0, 1).toUpperCase() + text.substring(1).toLowerCase();

        if (replaceUnderscore) {
            toReturn = toReturn.replace("_", " ");
        }

        return toReturn;
    }
}

Related

  1. capitalizeString(String aString)
  2. capitalizeString(String str)
  3. capitalizeString(String string)
  4. capitalizeString(String string)
  5. capitalizeString(String string)
  6. capitalizeString(String toCapitalize)
  7. capitalizeToUpper(String line)
  8. toCapitalized(Object obj)