Uppercase first letter of variable

后端 未结 23 1380
粉色の甜心
粉色の甜心 2020-11-30 20:55

I have searched over the web can can\'t find anything to help me. I want to make the first letter of each word upper case within a variable.

So far i have tried:

23条回答
  •  失恋的感觉
    2020-11-30 21:16

    I imagine you could use substring() and toUpperCase() to pull out the first character, uppercase it, and then replace the first character of your string with the result.

    myString = "cheeseburger";
    firstChar = myString.substring( 0, 1 ); // == "c"
    firstChar.toUpperCase();
    tail = myString.substring( 1 ); // == "heeseburger"
    myString = firstChar + tail; // myString == "Cheeseburger"
    

    I think that should work for you. Another thing to consider is that if this data is being displayed, you can add a class to its container that has the CSS property "text-transform: capitalize".

提交回复
热议问题