Java method to convert a string to proper or initial case

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;
}

Leave a Reply

Your email address will not be published.