How to get the string length in bytes in nodejs?

前端 未结 4 971
伪装坚强ぢ
伪装坚强ぢ 2021-02-01 12:03

How to get the string length in bytes in nodejs? If I have a string, like this: äáöü then str.length will return with 4. But how to get that, how many bytes form th

4条回答
  •  生来不讨喜
    2021-02-01 12:53

    Alternatively, you can use TextEncoder

    new TextEncoder().encode(str).length
    

    Related question

    Assume it's slower though

提交回复
热议问题