开发者

Changing list of NAMES TYPED IN CAPITALS to Names Typed In Capitals with Java

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):

  1. Use a library that already handles sentence type capitalization
  2. Split the sentence and then upper case the first letter of each word
  3. Regex for space + letter and replace the end of the pattern with a capitalized version
  4. 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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜