1var string = "ABCDEFG";
2var truncString = string.substring(0, 3); //Only keep the first 3 characters
3console.log(truncString); //output: "ABC"
1const truncate = (str, max, suffix) => str.length < max ? str : `${str.substr(0, str.substr(0, max - suffix.length).lastIndexOf(' '))}${suffix}`;
2
3// Example
4truncate('This is a long message', 20, '...');
1function truncateString(str, num) {
2 if (str.length > num) {
3 return str.slice(0, num) + "...";
4 } else {
5 return str;
6 }
7}
8