Javascript String truncate(maxLength)

Description

Javascript String truncate(maxLength)


String.prototype.truncate = function (maxLength) {
  return this.length > maxLength
    ? this.substring(0, maxLength) + '?'
    : this//ww w. j  a  v  a 2s  . c  o m
};

Javascript String truncate(maxlength)

/**/*w ww. ja va 2  s .c o  m*/
 * Truncates string and replaces last three characters with '...', if string length is more than maxlength
 * @memberof module:StringFunctions
 * @param {number} maxlength
 * @returns {string}
 */
String.prototype.truncate = function (maxlength) {
    if (!this) {
        return '';
    }
    if (!maxlength) {
        maxlength = 20;
    }
    return (this.length > maxlength) ? this.slice(0, maxlength - 3) + '...' : this;
};



PreviousNext

Related