I am using the substring method to limit the number of characters on each line. The issue I am facing is that the word breaks when the character limit is reached. I want whole words to be preserved. Are there any functions or logic which can limit by character count without breaking up words?
let MaxCharPerLine = 10;
let Inctext = "Sample text to print 10 charachters each line";
for (let i = 0; i < Inctext.length; i += MaxCharPerLine) {
console.log(Inctext.substring(i, i + MaxCharPerLine))
}
Output:
Sample tex
t to print
10 charac
hters each
line
Output I want:
Sample
text to
print 10
characters
each line