I have a string variable that will usually be several hundred characters long. It is basically a paragraph that will be displayed on a report and included in an electronic file. The electronic file requires that it be split into multiple lines, each of which are no longer than 80 characters. But I don't want it to split in the middle of the word.
One way I've tried it is to split on the space character and get an array of words. Then I create another array, where each element will be 1 line of text (for the e-file). Then I loop through the words, and for each one, if it will fit on the line without going over the max length, I add it to the line, and if not, I create another line and add it there.
This works as long as the max length for each chunk is greater than the length of each individual word. But what I'd like is to have a function to never exceed the max lenght for each chunk, even if it means it has to split the string somewhere other than on a space.
Any ideas?
One way I've tried it is to split on the space character and get an array of words. Then I create another array, where each element will be 1 line of text (for the e-file). Then I loop through the words, and for each one, if it will fit on the line without going over the max length, I add it to the line, and if not, I create another line and add it there.
This works as long as the max length for each chunk is greater than the length of each individual word. But what I'd like is to have a function to never exceed the max lenght for each chunk, even if it means it has to split the string somewhere other than on a space.
Any ideas?