How to capitalize first letter and lowercase the rest of the string

后端 未结 5 1224
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-01-20 05:22

I am trying to capitalize the first letter of only the first word in a sentence.

This is the data in the tsx file { this.text({ id: downloadPriceHistory

5条回答
  •  不知归路
    2021-01-20 06:15

    Why not just lowercase the entire string, and the uppercase just the first letter of the new string?

    function titleCase(string) {
        let sentence = string.toLowerCase();
        let titleCaseSentence = sentence.charAt(0).toUpperCase() + sentence.substring(1, sentence.length);
        return titleCaseSentence;
    }
    

    (Also, you're erasing your parameter to the function with that first line)

        string = 'hello World';
    

提交回复
热议问题