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

前端 未结 30 2214
南方客
南方客 2020-11-21 05:00

How do I make the first letter of a string uppercase, but not change the case of any of the other letters?

For example:

  • \"this is a test\"
30条回答
  •  遥遥无期
    2020-11-21 05:30

    The ucfirst function works if you do it like this.

    function ucfirst(str) {
        var firstLetter = str.slice(0,1);
        return firstLetter.toUpperCase() + str.substring(1);
    }
    

    Thanks J-P for the aclaration.

提交回复
热议问题