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

前端 未结 30 2902
南方客
南方客 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条回答
  •  萌比男神i
    2020-11-21 05:19

    Capitalize the first letter of all words in a string:

    function ucFirstAllWords( str )
    {
        var pieces = str.split(" ");
        for ( var i = 0; i < pieces.length; i++ )
        {
            var j = pieces[i].charAt(0).toUpperCase();
            pieces[i] = j + pieces[i].substr(1);
        }
        return pieces.join(" ");
    }
    

提交回复
热议问题