Javascript String truncate()

Description

Javascript String truncate()


String.prototype.truncate = 
  function(n){/*w w w.  jav  a2s. c o  m*/
      return this.substr(0,n-1)+(this.length>n?'...':'');
  };

Javascript String truncate()

String.prototype.truncate = function(){
  var truncated = this.slice(0, 101);
  if (truncated.length < 101) {
    return truncated
  } else {/*from  w ww .j  a va  2  s.  com*/
    for (var i = truncated.length; truncated[i] != " "; i--) {
      var trunc = truncated.slice(0, i)
    }
    return trunc + '...'
  }
}

String.prototype.capitalize = function() {
  return this.charAt(0).toUpperCase() + this.slice(1);
}



PreviousNext

Related