Lets say I have file with sever hundred lines of text all in capital letters. How should I go about changing the words on each line to lower case with only the first letter staying as capital?
TEXT ON FIST LINE
TEXT ON SECOND LINE
TEXT ON THIRD LINE
to
Text On Fist Line
Text On Second Line
Text On Third Line
I was thinking something like this
开发者_Python百科 s.substring(0, 1).toUpperCase() + s.substring(1).toLowerCase()
But using StringBuilder would be probably smarter and there is probably even something better than StringBuilder.
If you don't mind using apace commons, you can use WordUtils.capitalize()
or WordUtils.capitalizeFully()
Four options (actually 5, but one is a variation of another):
- Use a library that already handles sentence type capitalization
- Split the sentence and then upper case the first letter of each word
- Regex for space + letter and replace the end of the pattern with a capitalized version
- Run through the string and capitalize after a space is reached
The #5, a variation of #4, is still running through the string, but as binary. This is not difficult, but it changes based on whether this is ASCII or Unicode and may change due to character set.
If you do this with a custom routine, set it up as a reusable class, as I can almost guarantee you will use this again.
精彩评论