List of utility methods to do String Truncate
truncate(x)String.prototype.truncate = function(x){ return this.length > x ? this.substr(0,x) : this ; | |
truncate(x)String.prototype.truncate = function(x){ if(this.length < x) return this.valueOf(); else return this.substring(0, x - 3) + "..."; }; | |
truncateFilename()String.prototype.truncateFilename = function() { var fileParts = this.split('.'); var extension = fileParts[fileParts.length-1]; var filename = this.substr(0, this.length - extension.length + 1); return filename.substr(0, 10) + '*.' + extension; }; | |
truncateString(str, num)function truncateString(str, num) { if (str.length <= num) { return str; } else { return str.slice(0, num > 3 ? num - 3 : num) + '...'; truncateString("A-", 1); | |
truncateString(str, num)function truncateString(str, num) { if(str.length > num){ if(num < 3){ str = str.slice(0,num) + "..."; }else{ str = str.slice(0,num-3) + "..."; console.log("actual str is " + str); }else{ ... | |
truncateString(str, num)function truncateString(str, num) { var trunc; if (num <= 3) { if (num < str.length){ return str.slice(0,num) + "..."; } else{ trunc = str.slice(0,num); return trunc; } else if (num >= str.length) { return str; } else { trunc = str.slice(0,num - 3); return trunc + "..."; truncateString("A-tisket a-tasket A green and yellow basket", 11); |