I have name String : abc edf
i want to display fist letter of the all word in capital like : Abc Edf
how to do it ?
I have name String : abc edf
i want to display fist letter of the all word in capital like : Abc Edf
how to do it ?
As mentioned in the comments, there are many answers to this question. Just for fun I wrote my own method real quick. Feel free to use it and/or improve it:
public static String capitalizeAllWords(String str) {
String phrase = "";
boolean capitalize = true;
for (char c : str.toLowerCase().toCharArray()) {
if (Character.isLetter(c) && capitalize) {
phrase += Character.toUpperCase(c);
capitalize = false;
continue;
} else if (c == ' ') {
capitalize = true;
}
phrase += c;
}
return phrase;
}
Test:
String str = "this is a test message";
System.out.print(capitalizeAllWords(str));
Output:
This Is A Test Message