Get String char length - Node.js String

Node.js examples for String:Length

Description

Get String char length

Demo Code


String.prototype.charLength = function() {
  var len = 0;/*from   w w w  .  j  av a2 s  .co m*/
  for (var i = 0; i < this.length; i++) {
    var c = this.charCodeAt(i);
    if ((c >= 0x0001 && c <= 0x007e) || (0xff60 <= c && c <= 0xff9f))
      len += 1;
    else
      len += 2;
  }
  return len;
};

Related Tutorials