How to calculate byte length containing UTF8 characters using javascript?

前端 未结 3 1835
春和景丽
春和景丽 2021-02-13 21:35

I have textbox, in which the user can enter the characters in ASCII/UTF-8 or a combination of both. Is there any API in javascript which we can calculate the length of string in

3条回答
  •  广开言路
    2021-02-13 21:47

    As of 2018, the most compatible and reliable way of doing this seems to be with the blob api.

    new Blob([str]).size
    

    Even supported in IE10 if anyone uses that anymore.

提交回复
热议问题