View Single Post
Old 04-29-2004, 05:28 PM   #4 (permalink)
Belisarius
Java fanboy
 
Belisarius's Avatar
 
Join Date: Aug 2003
Posts: 1,161
Belisarius is on a distinguished road
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.
__________________
GitS
Belisarius is offline   Reply With Quote