Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

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}`;

// Example
truncate('This is a long message', 20, '...'); 
Source by stackoverflow.com #
 
PREVIOUS NEXT
Tagged: #javascript #truncate #string #full #word
ADD COMMENT
Topic
Name
6+1 =