How to convert the first letter of each word of a string to uppercase in Java

1 Answer

0 votes
public class MyClass {
    public static String convert_each_word_first_letter_to_uppercase(String s) {
        String[] words = s.split(" ");
        StringBuffer sb = new StringBuffer();
    
        for (int i = 0; i < words.length; i++) {
            sb.append(Character.toUpperCase(words[i].charAt(0)))
                .append(words[i].substring(1)).append(" ");
        }         
        
        return sb.toString().trim();
    }  
    public static void main(String args[]) {
        String s = "java c php python";
        
        s = convert_each_word_first_letter_to_uppercase(s);

        System.out.println(s);
    }
}



/*
run:

Java C Php Python

*/

 



answered Nov 14, 2019 by avibootz
...