I have a long string that I need to parse into an array of strings that do not exceed 40 characters in length. The tricky part of this for me is making sure that the regex finds the last whitespace before 40 characters to make a clean break between strings since I don’t want words cut off.
Share
Right-trim the substrings as you go:
The first alternative tries for a clean break, but the other is there as a fallback for blindly chopping if need be. Afterward, the substrings are available in
m.Groups["sub"].Captures.