I have a very long string consisting of words punctuation and spaces. I am trying to modify the string to add a new line (\n) after every x characters but while preserving whole words. For example:
An evil human sprayed innocent students in the face with pepper spray
With x = 20 would output like this:
An evil human sprayed 21 chars innocent students in 20 chars the face with pepper 20 chars spray end of string If words would be split by inserting a \n after x characters, then the word should be allowed to stay on the line, while a new line would be inserted at the end of the word.
Is there a catch-all way of doing this without a whole lot of complicated code?