Capitalize first letter a word [duplicated]

0

I want to capitalize the first letter of all the words in a phrase  EJ:

  

hello world

And the expected response is:

  

Hello World

I'm using java and I have not managed to generate a method that can do it.

    
asked by Stevn 19.11.2017 в 19:25
source

1 answer

5
public static String convierte(String string) {
    if (string == null) {
        return null;
    }
    StringBuilder builder = new StringBuilder();
    StringTokenizer st = new StringTokenizer(string," ");
    while (st.hasMoreElements()) {
        String ne = (String)st.nextElement();
        if (ne.length()>0) {
            builder.append(ne.substring(0, 1).toUpperCase());
            builder.append(ne.substring(1).toLowerCase()); //agregado
            builder.append(' ');
        }
    }
    return builder.toString();
}
    
answered by 19.11.2017 / 19:43
source