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

后端 未结 30 1369
情深已故
情深已故 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条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-11-22 02:21

    String s="hi dude i                                 want apple";
        s = s.replaceAll("\\s+"," ");
        String[] split = s.split(" ");
        s="";
        for (int i = 0; i < split.length; i++) {
            split[i]=Character.toUpperCase(split[i].charAt(0))+split[i].substring(1);
            s+=split[i]+" ";
            System.out.println(split[i]);
        }
        System.out.println(s);
    

提交回复
热议问题