CAN'T Use 'toUpperCase' (JS)

This question already has an answer here:

  • How do I make the first letter of a string uppercase in JavaScript? 69 answers

  • The best method I've found is just to call toUpperCase on the first character and concat the rest of the string using slice :

    function capitalize(str) {
      if(typeof str === 'string') {
        return str[0].toUpperCase() + str.slice(1);
      }
      return str;
    }
    

    If you want to capitalize each word in a sentence, you can split on space:

    "capitalize each word of this sentence".split(' ').map(capitalize).join(' ');
    
    链接地址: http://www.djcxy.com/p/17870.html

    上一篇: 使用Angular或typescript来首字母大写字母

    下一篇: 不能使用'toUpperCase'(JS)