views:

5076

answers:

5

I have a string: "hello good old world" and i want to upper case every first letter of every word, not the whole string with .toUpperCase(). Is there an existing java helper which does the job?

+5  A: 

i dont know if there is a function but this would do the job in case there is no exsiting one:

String s = "here are a bunch of words";

final StringBuilder result = new StringBuilder(s.length());
String[] words = s.split("\\s");
for(int i=0,l=words.length;i<l;++i) {
  if(i>0) result.append(" ");      
  result.append(Character.toUpperCase(words[i].charAt(0)))
        .append(words[i].substring(1));

}
tommyL
+10  A: 

Have a look at ACL WordUtils.

kd304
thanks, didn't know that class!
Chris
kd304
@kd i tryed http://www.google.at/search?q=java+word+uppercase thxn anyway
Chris
I guessed, but sometimes hard to get the terminology right. This is one of reasons SO exists.
kd304
A: 

its printing here here are here are a here are a bunch here are a bunch of here are a bunch of words..........

this is not required i guess

Salman Kazmi
I think maybe you meant to add this as a comment to tommyL's answer, not a new answer.
MatrixFrog
A: 

Also you can take a look into [StringUtils][1] library. It has a bunch of cool stuff.

[1]: http://commons.apache.org/lang/api/org/apache/commons/lang/StringUtils.html#remove(java.lang.String, char)

amoran
Did you understood the question? Did you check the accepted answer? Did you verify the link before posting? Did you check the topic's date?
BalusC
A: 

public String UpperCaseWords(String line) { line = line.trim().toLowerCase(); String data[] = line.split("\s"); line = ""; for(int i =0;i< data.length;i++) { if(data[i].length()>1) line = line + data[i].substring(0,1).toUpperCase()+data[i].substring(1)+" "; else line = line + data[i].toUpperCase(); } return line.trim(); }

Arun