Search the Site

Sponsors

bottom corner

Java method to convert a string to proper or initial case

This note provides a simple Java method for converting a string to have proper case, or in other words, each word will have its initial letter capitalized.

This page is filed under keyword(s): java.

Example: toProperCase("hello john smith") will return "Hello John Smith".

public static String toProperCase(String inputString) {
	String ret = "";
	StringBuffer sb = new StringBuffer();
	Matcher match = Pattern.compile("([a-z])([a-z]*)", Pattern.CASE_INSENSITIVE).matcher(inputString);
	while (match.find()) {
		match.appendReplacement(sb, match.group(1).toUpperCase() + match.group(2).toLowerCase()) ;
	}
	ret = match.appendTail(sb).toString();
	return ret;
}

Did you find this page useful? Please consider browsing other articles or subscribing to the RSS feed to keep up with latest.

This page is filed under keyword(s): java.
Author: C. Peter Chen
Last updated: 1 Jul 2010

bottom corner