Put the first letter of each word in uppercase

1

I want to standardize what the user typed in edittext, placing the first letter of each word in upper case and the rest of the letter lower case. How do I do this?

The part that directs the variables in the model class

Contato c = new Contato();
c.setNome(etNome.getText().toString());
    
asked by anonymous 15.09.2017 / 00:49

1 answer

4

You can use a combination of toUpperCase() for the first letter and toLowerCase() so that the rest of the word is all tiny. Excluding this in one method, it looks like this:

public String toTitledCase(String str){

    String[] words = str.split("\s");
    StringBuilder sb = new StringBuilder();

    for(int i = 0; i < words.length; i++){
        sb.append(words[i].substring(0, 1).toUpperCase() + words[i].substring(1).toLowerCase());
        sb.append(" ");
    }

    return sb.toString();
}

See working in ideone .

    
15.09.2017 / 00:53