Capitalize first letter of each word in JS

前端 未结 17 716
闹比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:48

    Or you could save a lot of time and use Lodash

    Look at
    https://lodash.com/docs/4.17.4#startCase -added/edited-
    https://lodash.com/docs/4.17.4#capitalize

    Ex.

    -added/edited-
    You may what to use startCase, another function for capitalizing first letter of each word.

    _.startCase('foo bar'); 
    // => 'Foo Bar'
    

    and capitalize for only the first letter on the sentence

    _.capitalize('FRED');
    // => 'Fred'
    

    Lodash is a beautiful js library made to save you a lot of time.

    There you will find a lot of time saver functions for strings, numbers, arrays, collections, etc.

    Also you can use it on client or server (nodejs) side, use bower or node, cdn or include it manually.

提交回复
热议问题