Javascript String split(separator, nbr)

Description

Javascript String split(separator, nbr)


String.prototype.split = function(separator, nbr) {
  var results = [];
  var pt = "";
  var ct = 0;//w w w. j a  v a  2  s . c o m
  for (var i = 0, len = this.length; i < len; i++) {
    if (this[i] == separator) {
      results.push(pt);
      pt = ""; ct++;
      if (nbr && ct >= nbr) {
        separator = "";
      }
    } else {
      pt += this[i];
    }
  }
  results.push(pt);
  
  return results;
};



PreviousNext

Related