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?
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));
}
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
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)
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(); }