Convert string to title case with JavaScript

后端 未结 30 2818
夕颜
夕颜 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:25

    Simple way to convert an individual word to title case

    Using the "Slice" method and String concatenation

    str.slice(0, 1).toUpperCase() + str.slice(1, str.length)
    

    *Add .toLowerCase() to the end if you want to lowercase the rest of the word

    Using ES6 Spread Operator, Map, and Join

    [...str].map((w, i) => i === 0 ? w[0].toUpperCase() : w).join('')
    
    0 讨论(0)
  • 2020-11-21 07:26

    I prefer the following over the other answers. It matches only the first letter of each word and capitalises it. Simpler code, easier to read and less bytes. It preserves existing capital letters to prevent distorting acronyms. However you can always call toLowerCase() on your string first.

    function title(str) {
      return str.replace(/(^|\s)\S/g, function(t) { return t.toUpperCase() });
    }
    

    You can add this to your string prototype which will allow you to 'my string'.toTitle() as follows:

    String.prototype.toTitle = function() {
      return this.replace(/(^|\s)\S/g, function(t) { return t.toUpperCase() });
    }
    

    Example:

    String.prototype.toTitle = function() {
      return this.replace(/(^|\s)\S/g, function(t) { return t.toUpperCase() });
    }
    
    console.log('all lower case ->','all lower case'.toTitle());
    console.log('ALL UPPER CASE ->','ALL UPPER CASE'.toTitle());
    console.log("I'm a little teapot ->","I'm a little teapot".toTitle());

    0 讨论(0)
  • 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'

    0 讨论(0)
  • 2020-11-21 07:27

    Try to apply the text-transform CSS style to your controls.

    eg: (text-transform: capitalize);

    0 讨论(0)
  • 2020-11-21 07:27

    Most of these answers seem to ignore the possibility of using the word boundary metacharacter (\b). A shorter version of Greg Dean's answer utilizing it:

    function toTitleCase(str)
    {
        return str.replace(/\b\w/g, function (txt) { return txt.toUpperCase(); });
    }
    

    Works for hyphenated names like Jim-Bob too.

    0 讨论(0)
  • 2020-11-21 07:28

    I made this function which can handle last names (so it's not title case) such as "McDonald" or "MacDonald" or "O'Toole" or "D'Orazio". It doesn't however handle German or Dutch names with "van" or "von" which are often in lower-case... I believe "de" is often lower-case too such as "Robert de Niro". These would still have to be addressed.

    function toProperCase(s)
    {
      return s.toLowerCase().replace( /\b((m)(a?c))?(\w)/g,
              function($1, $2, $3, $4, $5) { if($2){return $3.toUpperCase()+$4+$5.toUpperCase();} return $1.toUpperCase(); });
    }
    
    0 讨论(0)
提交回复
热议问题