0

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?

jshah
  • 33
  • 4
  • 1
    please share code what you have tried so far – Naga Sai A Mar 07 '19 at 20:32
  • 1
    If possible, please provide sample input and expected result – Naga Sai A Mar 07 '19 at 20:33
  • Why do you need to do this? Can't you just display your text in a container that has word wrapping and an appropriate width set? – James Mar 07 '19 at 20:48
  • @NagaSaiA I haven't tried anything so far – jshah Mar 07 '19 at 20:49
  • @James, unfortunately the container is been drawn on the fly in an SVG format by a library, so I don't have any control over it's wrapping or CSS. – jshah Mar 07 '19 at 20:52
  • @NagaSaiA here is the sample JSON response: "Content": "With thanks to the poet, Robert Frost, for the underlying structure.\r\nPlease note: This poem was created with our quick poem generator, so results are fairly basic. Our other generators use robots who take your input then trawl the dictionary for rhymes, synonymns and alliteration, creating a more unique experience." – jshah Mar 07 '19 at 20:58
  • Maybe see if [this](https://stackoverflow.com/questions/4991171/auto-line-wrapping-in-svg-text) can help – James Mar 07 '19 at 20:59
  • @James, Thanks but it won't help since I don't have access to the SVG code, it's controlled and generated by a library. – jshah Mar 07 '19 at 21:07

0 Answers0