Stanley Shilov

Sentence Capitalization with Java

Posted on Saturday, April 18th, 2009 under Coding

Simple Java function to capitalize the first word of every sentence:

public static String capitalize(String content){
	Pattern capitalize = Pattern.compile("([\\?!\\.] )([a-z])");
	Matcher m = capitalize.matcher(content);
	while (m.find()) {
		content = m.replaceFirst(m.group(1) + m.group(2).toUpperCase());
		m = capitalize.matcher(content);
	}
	return content;
}

If you have any suggestions on how to improve this method, leave a comment.

Tags: ,

You can leave a response, or trackback from your own site.

Leave a Reply



« »
© 2010 Stanley Shilov   |   Blog   |   Contact

divide et impera

Stanley Shilov