Here you can find the source of toTitleCase(final String inStr, final boolean putRestInLC)
Parameter | Description |
---|---|
inStr | string to put in title case |
putRestInLC | whether the rest of the string should be made lowercase |
public static String toTitleCase(final String inStr, final boolean putRestInLC)
//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()); } } }