Adds chunking method to strings. Splits string into an array containing substrings of n chars each - Node.js String

Node.js examples for String:Split

Description

Adds chunking method to strings. Splits string into an array containing substrings of n chars each

Demo Code

String.prototype.chunk = function(n) {
    var ret = [];
    //from   ww w.ja  v a2  s. c  o m
  for(var i=0, len=this.length; i < len; i += n) {
     ret.push(this.substr(i, n))
  }

    return ret;
};

Related Tutorials