Java String Title Case toTitleCase(final String inStr)

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

Description

Converts the string to title case.

License

Open Source License

Parameter

Parameter Description
inStr the string to make upper case

Return

the string parameter, in upper case

Declaration

public static String toTitleCase(final String inStr) 

Method Source Code

//package com.java2s;
/**//w ww .  j  a  v a  2  s  .c  o m
 * Written by Mike Wallace (mfwallace at gmail.com).  Available
 * on the web site http://mfwallace.googlepages.com/.
 * 
 * Copyright (c) 2006 Mike Wallace.
 * 
 * Permission is hereby granted, free of charge, to any person
 * obtaining a copy of this software and associated documentation
 * files (the "Software"), to deal in the Software without
 * restriction, including without limitation the rights to use,
 * copy, modify, merge, publish, distribute, sublicense, and/or
 * sell copies of the Software, and to permit persons to whom
 * the Software is furnished to do so, subject to the following
 * conditions:
 * 
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 * OTHER DEALINGS IN THE SOFTWARE.
 */

public class Main {
    /**
     * Converts the string to title case.
     * 
     * @param inStr the string to make upper case
     * @return the string parameter, in upper case
     */
    public static String toTitleCase(final String inStr) {
        // 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
            StringBuilder buf = new StringBuilder(inStr.toLowerCase());

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

                // If we hit a space, set a flag so we make the next non-space
                // char uppercase
                if ((ch == ' ') || (ch == '(') || (ch == '-') || (ch == '/')) {
                    blankFound = true;
                    continue;
                } else {
                    // Check if it's lowercase and the last character was a space
                    if (blankFound) {
                        // It is, so make it uppercase and replace in the buffer
                        // ch = Character.toUpperCase(ch);
                        buf.setCharAt(nIndex, Character.toUpperCase(ch));
                    }

                    // Clear the flag
                    blankFound = false;
                }
            }

            // Make it a string
            String outStr = buf.toString();

            // Return it
            return outStr;
        }
    }
}

Related

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