Javascript String chunk(n)

Description

Javascript String chunk(n)


// Allows us to break text into chunks of sepecified size.
String.prototype.chunk = function(n)
{
 if (typeof n=='undefined')
 {
  n=2;//from ww w  .  j a  v a  2  s . c  o m
 }
 return this.match(RegExp('.{1,'+n+'}','g'));
};

Javascript String chunk(n)

String.prototype.chunk = function (n) {
  var ret = [];//from   w  ww  . j  a  va  2 s . com
  for (var i = 0, len = this.length; i < len; i += n) {
    ret.push(this.substr(i, n));
  }
  return ret;
};



PreviousNext

Related