Skip to content

Instantly share code, notes, and snippets.

@khanzadimahdi
Created April 23, 2019 05:46
Show Gist options
  • Save khanzadimahdi/1d0b646e8c88c413a9d0325e0e03438e to your computer and use it in GitHub Desktop.
Save khanzadimahdi/1d0b646e8c88c413a9d0325e0e03438e to your computer and use it in GitHub Desktop.
limit strings length in pure javascript
// add some prototypes
String.prototype.limit = function(length = 150, tail = '...') {
return this.substr(0, length) + (this.length > length ? tail : '');
};
// how to use:
'this is an string'.limit(5);
// or it can be like the below
let name = 'this is my name';
alert( name.limit(10) );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment