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

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

    I didn’t see any mention in the existing answers of issues related to astral plane code points or internationalization. “Uppercase” doesn’t mean the same thing in every language using a given script.

    Initially I didn’t see any answers addressing issues related to astral plane code points. There is one, but it’s a bit buried (like this one will be, I guess!)


    Most of the proposed functions look like this:

    function capitalizeFirstLetter(str) {
      return str[0].toUpperCase() + str.slice(1);
    }
    

    However, some cased characters fall outside the BMP (basic multilingual plane, code points U+0 to U+FFFF). For example take this Deseret text:

    capitalizeFirstLetter("

提交回复
热议问题