Java String Title Case toTitleCase(final String inStr, final boolean putRestInLC)

Here you can find the source of toTitleCase(final String inStr, final boolean putRestInLC)

Description

Convert a string to title case.

License

Open Source License

Parameter

Parameter Description
inStr string to put in title case
putRestInLC whether the rest of the string should be made lowercase

Return

the input string in title case

Declaration

public static String toTitleCase(final String inStr, final boolean putRestInLC) 

Method Source Code

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

public class Main {
    /**/*ww w. j a  va2 s . c  o  m*/
     * Convert a string to title case.
     *
     * @param inStr string to put in title case
     * @param putRestInLC whether the rest of the string
     *                    should be made lowercase
     * @return the input string in title case
     */
    public static String toTitleCase(final String inStr, final boolean putRestInLC) {
        // Check for a null or empty string
        if ((inStr == null) || (inStr.length() < 1)) {
            return "";
        } else {
            // Save the length
            final int nLen = inStr.length();

            // If one character, make it uppercase and return it
            if (nLen == 1) {
                return inStr.toUpperCase();
            }

            // Set this to true because we want to make the first character uppercase
            boolean blankFound = true;

            // Save the string to a stringbuffer
            StringBuffer buf = new StringBuffer(inStr);

            // Traverse the character array
            for (int nIndex = 0; nIndex < nLen; ++nIndex) {
                // Save the current character
                final char ch = buf.charAt(nIndex);

                // If we hit a space, set a flag so we make the next non-space char uppercase
                if (ch == ' ') {
                    blankFound = true;
                    continue;
                } else {
                    // See if the previous character was a space
                    if (blankFound) {
                        // Check if this is lowercase
                        if (Character.isLowerCase(ch)) {
                            // Make the character uppercase
                            buf.setCharAt(nIndex, Character.toUpperCase(ch));
                        }
                    } else {
                        // Check if the rest the string should be made lowercase
                        if (putRestInLC) {
                            // Check if this is uppercase
                            if (Character.isUpperCase(ch)) {
                                // Make the character lowercase
                                buf.setCharAt(nIndex, Character.toLowerCase(ch));
                            }
                        }
                    }

                    // Clear the flag
                    blankFound = false;
                }
            }

            // Return it
            return (buf.toString());
        }
    }
}

Related

  1. titleCasedName(T instance)
  2. titleCaseTruncate(String s, int maxlen)
  3. toTitleCase(final int chr)
  4. toTitleCase(final String input)
  5. toTitleCase(final String inStr)
  6. toTitleCase(final String s)
  7. toTitleCase(final String text)
  8. toTitleCase(String givenString)
  9. toTitleCase(String input)