Here's how I would do it:
Code:
public static String ucFirst(String strText){
if(strText == null || strText.length() == 0){
return "";
}
StringTokenizer toks = new StringTokenizer(strText);
StringBuffer output = new StringBuffer();
while(toks.hasMoreTokens()){
String word = toks.nextToken();
output.append(word.substring(0,1).toUpperCase());
if(word.length() > 1){
output.append(word.substring(1).toLowerCase());
}
output.append(" ");
}
return output.toString();
}
Hopefully you're not worried about instances where you might want a capitalize letter in the middle of the word, like DuPont.