Skip to content

Instantly share code, notes, and snippets.

@giorgiofellipe
Last active March 22, 2016 03:15
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save giorgiofellipe/7b436f73a1e4e88506fd to your computer and use it in GitHub Desktop.
Save giorgiofellipe/7b436f73a1e4e88506fd to your computer and use it in GitHub Desktop.
Text format
var yourString = "The quick brown fox jumps over the lazy dog"; //replace with your string.
var finalString = "";
var maxLength = 16 // maximum number of characters to extract
var count = 0;
var maxTries = ((yourString.length / maxLength) * 2);
while (yourString.length > maxLength) {
count++;
if (count > maxTries) {
break;
}
//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(" ")));
yourString = yourString.replace(trimmedString + " ", "");
finalString += trimmedString + '<br>';
}
finalString += yourString;
console.log(yourString);
console.log(finalString);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment