A rags-to-riches of A method to capitalize the first letter of all words in a String.
To recap the requirements:
Please implement this method to capitalize all first letters of the words in the given String. All other symbols shall remain intact. If a word starts not with a letter, it shall remain intact too. Assume that the parameter String can only contain spaces and alphanumeric characters.
NOTE: please keep in mind that the words can be divided by single or multiple spaces.The spaces also can be found at the beginning or the end of the parameter string,and you need to preserve them.
And the code:
public class Main
{
public static void main(String... args) {
for(String arg : args) {
System.out.println(titleize(arg));
}
}
/**
* Titleize a string. Takes a string and returns a new string where all words
* have had their first letter title cased. If the letter is already title case
* or is not a cased letter (like a number), it will be passed through.
* Leading, trailing, and all other whitespace is preserved.
*
* This method is not as robust as titleize in Rails. It does not do any magic
* like breaking up MashedTogetherWords or replacing_underscores.
*
* This will blow up if null is passed in.
*/
public static String titleize(final String input) {
// Initialize the output to the length of the input since we know it and the
// output and input will be the same size.
StringBuilder output = new StringBuilder(input.length());
// This defaults to true because we assume the beginning of the
// string also counts as whitespace. This is to make sure the
// first word gets capitalized as well.
boolean lastCharacterWasWhitespace = true;
for(int i = 0; i < input.length(); i++) {
char currentCharacter = input.charAt(i);
if(lastCharacterWasWhitespace && Character.isLowerCase(currentCharacter)) {
currentCharacter = Character.toTitleCase(currentCharacter);
}
output.append(currentCharacter);
lastCharacterWasWhitespace = Character.isWhitespace(currentCharacter);
}
return output.toString();
}
}
The original code tried to use regular expressions and splitting words to accomplish the task. This does neither. It was partially inspired by the accepted answer, by treating the String
as a stream of characters.
This is Java 7. I'm sure there's some Java 8 stream fanciness that can do this in half the lines.