I get a long string (upto 2000 characters which might also include special characters) back from the server. The string might or might not have the "\r" "Enter" character for new line. The goal is to insert the "\r" "Enter" character after a set number of characters (i.e. 50 characters), but without breaking any words.
Here are the steps I need to follow:
1) Check if "\r" is there within first 50 characters.
2) If it's there, Do Nothing and check next 50 characters immediately after "\r" is found.
3) If it's not there, find the very first "space" between the words after 50 characters and insert "\r" immediately after it.
4) Do this for the entire string to make sure there is a line break ("\r") after every 50 characters, while preserving all other special characters present in the string.
Does anyone has a faster solution to achieve this?