Calculate the number of bytes of a Unicode string - Node.js String

Node.js examples for String:Unicode

Description

Calculate the number of bytes of a Unicode string

Demo Code


// $Id: uc_taxes.js,v 1.10.2.3 2008/11/03 21:33:53 islandusurper Exp $

/**//from   w w  w  .  j a  v  a2  s . c  om
 * Calculate the number of bytes of a Unicode string.
 *
 * Gratefully stolen from http://dt.in.th/2008-09-16.string-length-in-bytes.html
 */
String.prototype.bytes = function() {
    return Drupal.encodeURIComponent(this).replace(/%../g, 'x').length;
};

Related Tutorials