Javascript String slice(start, end)

Description

Javascript String slice(start, end)


String.prototype.slice = function(start, end) {
  if (start < 0) {
    start = start + this.length;/*from  w  ww.java  2s.  c  o m*/
  }
  if (!end) {
    end = this.length;
  } else if (end < 0) {
    end = end + this.length;
  }
  
  var s = "";
  for (var i = start, len = this.length; i < end && i < len; i++) {
    s += this[i];
  }
  
  return s;
};



PreviousNext

Related