There is a string, it is necessary to split it into substrings according to 2 conditions:
- The length of the substring (it must not exceed the size of the
chunk) - By entering a newline control character
\n
That is, it is necessary to break the line into blocks, but at the same time not as horrible, but so that the blocks begin and end with new lines, without breaks.
I wrote this algorithm, but somehow it turned out too cumbersome. I have been working for quite a long time, so the inspiration has dried up and the eye has already become blurred. Please help.
var chunk = 10; var s = "long-long\n string\n with some\n chars"; var messages = new List<string>(); for (var i = chunk;; i += chunk) { if (i >= s.Length) { messages.Add(s.Substring(i + 1 - chunk)); break; } i = s.LastIndexOf('\n', i >= s.Length ? s.Length - 1 : i, 200); if (i <= chunk) { messages.Add(s.Substring(0, i)); continue; } var len = messages.Last().Length; messages.Add(s.Substring(len, i - len)); }
\nyou need to break necessarily + each resulting piece is divided by the length of the line? Can the gap be anywhere or only by the gap? - Andrey NOP\n- Anatols = "aaa\nbbb\nccc\nddd\neee\nfff\nggg\n"; chunk = 10s = "aaa\nbbb\nccc\nddd\neee\nfff\nggg\n"; chunk = 10ands = "aaaaaaaaaaaaaaa\nbbbbbbbbbbbbbbb\nccccccccccccccc\n" ; chunk = 10s = "aaaaaaaaaaaaaaa\nbbbbbbbbbbbbbbb\nccccccccccccccc\n" ; chunk = 10. - PetSerAl\n? But not necessarily on each, the main thing - that the pieces were no morechunk, right? - Andrey NOP