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.
To capitalize the first character of a string, We can use the charAt() to separate the first character and then use the toUpperCase() function to capitalize it.
To use a keyboard shortcut to change between lowercase, UPPERCASE, and Capitalize Each Word, select the text and press SHIFT + F3 until the case you want is applied.
string capitalize() in Python In Python, the capitalize() method returns a copy of the original string and converts the first character of the string to a capital (uppercase) letter while making all other characters in the string lowercase letters.
WordUtils.capitalize(str)
(from apache commons-text)
(Note: if you need "fOO BAr"
to become "Foo Bar"
, then use capitalizeFully(..)
instead)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With