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

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

    If you are wanting to reformat all-caps text, you might want to modify the other examples as such:

    function capitalize (text) {
        return text.charAt(0).toUpperCase() + text.slice(1).toLowerCase();
    }
    

    This will ensure that the following text is changed:

    TEST => Test
    This Is A TeST => This is a test
    

提交回复
热议问题