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

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

    Reusable method for intiCap:

        public class YarlagaddaSireeshTest{
    
        public static void main(String[] args) {
            String FinalStringIs = "";
            String testNames = "sireesh yarlagadda test";
            String[] name = testNames.split("\\s");
    
            for(String nameIs :name){
                FinalStringIs += getIntiCapString(nameIs) + ",";
            }
            System.out.println("Final Result "+ FinalStringIs);
        }
    
        public static String getIntiCapString(String param) {
            if(param != null && param.length()>0){          
                char[] charArray = param.toCharArray(); 
                charArray[0] = Character.toUpperCase(charArray[0]); 
                return new String(charArray); 
            }
            else {
                return "";
            }
        }
    }
    

提交回复
热议问题