Stanley Shilov

Sentence Capitalization with Java

April 18th, 2009

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.

© 2012 Stanley Shilov   |   Blog   |   Contact

divide et impera

Stanley Shilov