Posts Tagged ‘strings’

This is a handy little string function I use all the time to ellipse strings in a page. Bascially you give it the total length you want and it truncates and adds the ellipses to the end of it. It extends the string class so it is available as a method off the string object.

Usage:

var str = 'This is a really long string';
str = str.ellipse(10);
//str is: 'This is...' total of 10 chars

 

string.prototype.ellipse = function (stringLength) {
  if (this.length > stringLength - 3)
    return this.substring(0, stringLength - 3) + '...';
  else
    return this;
};
Advertisements