Stack Overflow archive
0 scoreaccepted

How to Display First letter of Word capital in to the name android

score
0
question views
538
license
CC BY-SA 3.0

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:

java
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:

java
String str = "this is a test message";
System.out.print(capitalizeAllWords(str));

Output:

java
This Is A Test Message

Originally posted on Stack Overflow. Public user contributions are licensed under Creative Commons Attribution-ShareAlike.