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)); } 
  • Those. On each \n you 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
  • No, you need to break along the length, but so that the gap was at the nearest \n - Anatol
  • @Anatol Can you give specific examples? For example, s = "aaa\nbbb\nccc\nddd\neee\nfff\nggg\n"; chunk = 10 s = "aaa\nbbb\nccc\nddd\neee\nfff\nggg\n"; chunk = 10 and s = "aaaaaaaaaaaaaaa\nbbbbbbbbbbbbbbb\nccccccccccccccc\n" ; chunk = 10 s = "aaaaaaaaaaaaaaa\nbbbbbbbbbbbbbbb\nccccccccccccccc\n" ; chunk = 10 . - PetSerAl
  • 2
    If I understood correctly: you need to take a line, look for the closest \ n to the hundredth character, cut off at the point of the break and repeat the operation until the rest of the line climbs completely into the buffer. So? And what to do if in some next chunk there are no line feeds at all? - AK ♦
  • Those. gap is possible only by \n ? But not necessarily on each, the main thing - that the pieces were no more chunk , right? - Andrey NOP

1 answer 1

If I understand you correctly, you can first break the string into pieces by \n , and then enlarge them to the required length:

 var chunk = 10; var s = "long-long\n string\n XX\nwith some\n chars"; // Π Π°Π·Π±ΠΈΠ²Π°Π΅ΠΌ Π½Π° куски ΠΏΠΎ Ρ€Π°Π·Π΄Π΅Π»ΠΈΡ‚Π΅Π»ΡŽ string[] parts = s.Split('\n'); // Бписок с Π³ΠΎΡ‚ΠΎΠ²Ρ‹ΠΌΠΈ сообщСниями, собранными ΠΈΠ· кусков var messages = new List<string>(); // Π’Π΅ΠΊΡƒΡ‰Π΅Π΅ сообщСниС, ΠΊΠΎΡ‚ΠΎΡ€ΠΎΠ΅ собираСм ΠΈΠ· кусков string message = string.Empty; foreach (string part in parts) { if (message.Length + part.Length + 1 > chunk) { // Если Ρ‚Π΅ΠΊΡƒΡ‰ΠΈΠΉ кусок Π½Π΅ помСщаСтся Π² message ΠΏΠΎ ΠΎΠ³Ρ€Π°Π½ΠΈΡ‡Π΅Π½ΠΈΡŽ Π΄Π»ΠΈΠ½Ρ‹ messages.Add(message); message = part + "\n"; } else message += part + "\n"; } // Если Π² message Ρ‡Ρ‚ΠΎ-Ρ‚ΠΎ ΠΎΡΡ‚Π°Π»ΠΎΡΡŒ messages.Add(message); 

If I get you wrong, and you need to receive messages that are guaranteed not to exceed the length limit, you can do this:

 foreach (string part in parts) { string message = part + "\n"; while (message.Length > chunk) { messages.Add(message.Substring(0, chunk)); message = message.Substring(chunk); } if (message.Length != 0) messages.Add(message); }