Convert string to title case with JavaScript

后端 未结 30 2835
夕颜
夕颜 2020-11-21 06:40

Is there a simple way to convert a string to title case? E.g. john smith becomes John Smith. I\'m not looking for something complicated like John R

30条回答
  •  醉话见心
    2020-11-21 07:26

    If you can use third party libraries in your code then lodash has a helper function for us.

    https://lodash.com/docs/4.17.3#startCase

    _.startCase('foo bar');
    // => 'Foo Bar'
    
    _.startCase('--foo-bar--');
    // => 'Foo Bar'
     
    _.startCase('fooBar');
    // => 'Foo Bar'
     
    _.startCase('__FOO_BAR__');
    // => 'FOO BAR'

提交回复
热议问题