Capitalize first letter of each word in JS

前端 未结 17 627
闹比i
闹比i 2021-01-03 23:26

I\'m learning how to capitalize the first letter of each word in a string and for this solution I understand everything except the word.substr(1) portion. I see that it\'s a

17条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-01-03 23:52

    function titlecase(str){
       let titlecasesentence = str.split(' ');
       titlecasesentence = titlecasesentence.map((word)=>{
         const firstletter = word.charAt(0).toUpperCase();
         word = firstletter.concat(word.slice(1,word.length));
    
         return word;
    });
      titlecasesentence = titlecasesentence.join(' ');
      return titlecasesentence;
    }
    titlecase('this is how to capitalize the first letter of a word');
    

提交回复
热议问题