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

前端 未结 30 2331
南方客
南方客 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:20

    function capitalize(s) {
        // returns the first letter capitalized + the string from index 1 and out aka. the rest of the string
        return s[0].toUpperCase() + s.substr(1);
    }
    
    
    // examples
    capitalize('this is a test');
    => 'This is a test'
    
    capitalize('the Eiffel Tower');
    => 'The Eiffel Tower'
    
    capitalize('/index.html');
    => '/index.html'
    

提交回复
热议问题