Calculates the length of the string in utf-8 - Node.js String

Node.js examples for String:Unicode

Description

Calculates the length of the string in utf-8

Demo Code

if (typeof String.prototype.utf8ByteLength != 'function')
{
  String.prototype.utf8ByteLength = function()
  {//  w  w w  . ja  va  2s  .  c o m
    // Matches only the 10.. bytes that are non-initial characters in a multi-byte sequence.
    var m = encodeURIComponent(this).match(/%[89ABab]/g);
    return this.length + (m ? m.length : 0);
  };
};

Related Tutorials