Here you can find the source of toTitleCase(final String inStr)
Parameter | Description |
---|---|
inStr | the string to make upper case |
public static String toTitleCase(final String inStr)
//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; } } }