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

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

    package corejava.string.intern;
    
    import java.io.DataInputStream;
    
    import java.util.ArrayList;
    
    /*
     * wap to accept only 3 sentences and convert first character of each word into upper case
     */
    
    public class Accept3Lines_FirstCharUppercase {
    
        static String line;
        static String words[];
        static ArrayList list=new ArrayList();
    
        /**
         * @param args
         */
        public static void main(String[] args) throws java.lang.Exception{
    
            DataInputStream read=new DataInputStream(System.in);
            System.out.println("Enter only three sentences");
            int i=0;
            while((line=read.readLine())!=null){
                method(line);       //main logic of the code
                if((i++)==2){
                    break;
                }
            }
            display();
            System.out.println("\n End of the program");
    
        }
    
        /*
         * this will display all the elements in an array
         */
        public static void display(){
            for(String display:list){
                System.out.println(display);
            }
        }
    
        /*
         * this divide the line of string into words 
         * and first char of the each word is converted to upper case
         * and to an array list
         */
        public static void method(String lineParam){
            words=line.split("\\s");
            for(String s:words){
                String result=s.substring(0,1).toUpperCase()+s.substring(1);
                list.add(result);
            }
        }
    
    }
    

提交回复
热议问题