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.
  1. Ramu Says:

    Good work dude, The initial letter is not capitalizing,
    Ex:
    String test = “hello every one. this is java capitalize . Is is working?.”;
    test = test.toLowerCase();
    System.out.println(capitalize(test));

    I get “hello every one. This is java capitalize . Is is working?.”

Leave a Reply



« »
© 2012 Stanley Shilov   |   Blog   |   Contact

divide et impera

Stanley Shilov