Convert String to Proper or Title case

Share on Facebook0Share on LinkedIn0Share on Google+0Tweet about this on TwitterEmail this to someoneDigg this

public static String toTitleCase(String input) {
final String ACTIONABLE_DELIMITERS = ” ‘-/”; // these cause the character following
// to be capitalized

StringBuilder sb = new StringBuilder();
boolean capNext = true;

for (char c : input.toCharArray()) {
c = (capNext)
? Character.toUpperCase(c)
: Character.toLowerCase(c);
sb.append(c);
capNext = (ACTIONABLE_DELIMITERS.indexOf((int) c) >= 0); // explicit cast not needed
}
return sb.toString();
}


Leave a Reply

Your email address will not be published. Required fields are marked *