javascript truncate string code example
Example 1: How to make string shorter javascript
const string = "Name".slice(0, 250).concat('...');
const string2 = "Name".substring(0, 250).concat('...');
Example 2: javascript truncate string full word
const truncate = (str, max, suffix) => str.length < max ? str : `${str.substr(0, str.substr(0, max - suffix.length).lastIndexOf(' '))}${suffix}`;
truncate('This is a long message', 20, '...');
Example 3: javascript truncate string
var string = "ABCDEFG";
var truncString = string.substring(0, 3);
console.log(truncString);
Example 4: truncate javascript
function truncateString(str, num) {
if (num > str.length){
return str;
} else{
str = str.substring(0,num);
return str+"...";
}
}
res = truncateString("A-tisket a-tasket A green and yellow basket", 11);
alert(res)
Example 5: substring
const str = 'Mozilla';
console.log(str.substring(1, 3));
console.log(str.substring(2));
Example 6: how to truncate string in javascript
function truncateString(str, num) {
if (str.length > num) {
return str.slice(0, num) + "...";
} else {
return str;
}
}