How to capitalize the first character of each word in a string

后端 未结 30 1373
情深已故
情深已故 2020-11-22 02:08

Is there a function built into Java that capitalizes the first character of each word in a String, and does not affect the others?

Examples:

  • jon
30条回答
  •  迷失自我
    2020-11-22 02:47

    Here is a simple function

    public static String capEachWord(String source){
        String result = "";
        String[] splitString = source.split(" ");
        for(String target : splitString){
            result += Character.toUpperCase(target.charAt(0))
                    + target.substring(1) + " ";
        }
        return result.trim();
    }
    

提交回复
热议问题