Part of this method is taken from an existing post I published on how to capitalize the first letter of every sentence in a String using Java. I used the same logic to incorporate it into the contents of a Document object.

While I used StyledDocument in the title of my post, this can actually work on other kind of documents like HTMLDocument. You just need to add a DocumentListener to the document object and pass the DocumentEvent as the parameter.

Here is a method I found in the Internet made by a certain Dogstopper of where every first letter of a sentence is capitalized using Java. I have my own but it uses Regex so I decided to post this one here for future reference.

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...