Last active
March 22, 2016 03:15
-
-
Save giorgiofellipe/7b436f73a1e4e88506fd to your computer and use it in GitHub Desktop.
Text format
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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