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

后端 未结 30 1433
情深已故
情深已故 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:39

    This is just another way of doing it:

    private String capitalize(String line)
    {
        StringTokenizer token =new StringTokenizer(line);
        String CapLine="";
        while(token.hasMoreTokens())
        {
            String tok = token.nextToken().toString();
            CapLine += Character.toUpperCase(tok.charAt(0))+ tok.substring(1)+" ";        
        }
        return CapLine.substring(0,CapLine.length()-1);
    }
    

提交回复
热议问题