Answers for "Shorten String without cutting word"

0

Shorten String without cutting word

var yourString = "The quick brown fox jumps over the lazy dog"; //replace with your string.
var maxLength = 6 // maximum number of characters to extract

//trim the string to the maximum length
var trimmedString = yourString.substr(0, maxLength);

//re-trim if we are in the middle of a word
trimmedString = trimmedString.substr(0, Math.min(trimmedString.length, trimmedString.lastIndexOf(" ")))
Posted by: Guest on September-02-2021

Code answers related to "Javascript"

Browse Popular Code Answers by Language