Created
April 23, 2019 05:46
-
-
Save khanzadimahdi/1d0b646e8c88c413a9d0325e0e03438e to your computer and use it in GitHub Desktop.
limit strings length in pure javascript
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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