Capitalize First Letter Of Every Sentence In Java

Here is a method in Java that capitalizes the first letter of every sentence found. This is made possible through the use of regular expressions (Regex).

The regex string in this method capitalizes the first letter found after either a period, question mark or exclamation point. The number of space characters in between does not matter so if you have 2 or 3 spaces after a period, the very first alphabet character it can find will be capitalized.

The drawback of this regular expression is that the very first letter of the string does not get capitalized. Hence, I added a little workaround code after the regex operation to make sure the first letter of the String is capitalized.

Related Posts Plugin for WordPress, Blogger...

5 comments

  1. Hi, thanks for the post, it helps a lot. I just wonder, there is anyway for doing this but for a html text? I mean, ignoring html tags, for example:

    “The drawback of this regular expression is that the very first letter of the string does not get capitalized. hence, I added a little workaround code after the regex operation to make sure the first letter of the String is capitalized.”

    I need to capitalize “hence” ignoring all html tags after the period.

    Any ideas?

    Thanks

Leave a Reply

Your email address will not be published. Required fields are marked *