不能使用'toUpperCase'(JS)

这个问题在这里已经有了答案:

  • 如何在JavaScript中制作字符串大写的第一个字母? 69个答案

  • 我发现的最好的方法就是在第一个字符上调用toUpperCase ,并使用slice对字符串的其余部分进行连接:

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

    如果你想在句子中大写每个单词,你可以分割空间:

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

    上一篇: CAN'T Use 'toUpperCase' (JS)

    下一篇: First letter uppercase, the rest lower case