Count bytes in textarea using javascript

后端 未结 10 628
难免孤独
难免孤独 2020-12-02 11:15

I need to count how long in bytes a textarea is when UTF8 encoded using javascript. Any idea how I would do this?

thanks!

10条回答
  •  情书的邮戳
    2020-12-02 11:32

    Combining various answers, the following method should be fast and accurate, and avoids issues with invalid surrogate pairs that can cause errors in encodeURIComponent():

    function getUTF8Length(s) {
      var len = 0;
      for (var i = 0; i < s.length; i++) {
        var code = s.charCodeAt(i);
        if (code <= 0x7f) {
          len += 1;
        } else if (code <= 0x7ff) {
          len += 2;
        } else if (code >= 0xd800 && code <= 0xdfff) {
          // Surrogate pair: These take 4 bytes in UTF-8 and 2 chars in UCS-2
          // (Assume next char is the other [valid] half and just skip it)
          len += 4; i++;
        } else if (code < 0xffff) {
          len += 3;
        } else {
          len += 4;
        }
      }
      return len;
    }
    

提交回复
热议问题