how to convert a starting character of each string into capital letter. example:
input:time to think
Output:Time To Think
how to convert a starting character of each string into capital letter. example:
input:time to think
Output:Time To Think
public static String capitalizeString(String string) {
char[] chars = string.toLowerCase().toCharArray();
boolean found = false;
for (int i = 0; i < chars.length; i++) {
if (!found && Character.isLetter(chars[i])) {
chars[i] = Character.toUpperCase(chars[i]);
found = true;
} else if (Character.isWhitespace(chars[i]) || chars[i]=='.' || chars[i]=='\'') { // You can add other chars here
found = false;
}
}
return String.valueOf(chars);
}
This should help you.
WordUtils.capitalize(str) (from apache commons-lang)
(Note: if you need "fOO BAr" to become "Foo Bar", then use capitalizeFully(..) instead)
or you can also do it as,
String str="hello how are you".trim();
String[] arr = str.split(" ");
StringBuffer sb = new StringBuffer();
for (int i = 0; i < arr.length; i++) {
sb.append(Character.toUpperCase(arr[i].charAt(0)))
.append(arr[i].substring(1)).append(" ");
}
System.out.print(sb.toString());
WordUtils.capitalize(str)
in apache commons should do the trick http://commons.apache.org/proper/commons-lang/apidocs/org/apache/commons/lang3/text/WordUtils.html#capitalize(java.lang.String)