Is there a function built into Java that capitalizes the first character of each word in a String, and does not affect the others?
Examples:
jon skeet
->Jon Skeet
miles o'Brien
->Miles O'Brien
(B remains capital, this rules out Title Case)old mcdonald
->Old Mcdonald
*
*(Old McDonald
would be find too, but I don't expect it to be THAT smart.)
A quick look at the Java String Documentation reveals only toUpperCase()
and toLowerCase()
, which of course do not provide the desired behavior. Naturally, Google results are dominated by those two functions. It seems like a wheel that must have been invented already, so it couldn't hurt to ask so I can use it in the future.
The following method converts all the letters into upper/lower case, depending on their position near a space or other special chars.
I'm using the following function. I think it is faster in performance.
The short and precise way is as follows:
It works without error if you try and change the name value to the three of values. Error free.
This one works for the surname case...
With different types of separators, and it keeps the same separator:
jean-frederic --> Jean-Frederic
jean frederic --> Jean Frederic
The code works with the GWT client side.
This might be useful if you need to capitalize titles. It capitalizes each substring delimited by
" "
, except for specified strings such as"a"
or"the"
. I haven't ran it yet because it's late, should be fine though. Uses Apache CommonsStringUtils.join()
at one point. You can substitute it with a simple loop if you wish.