// Collection of code snippets by Arne Vajhøj // (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) using System; using System.Text; namespace E { public class MainClass { public static string SplitLines(string s, int maxlen, string del) { StringBuilder res = new StringBuilder(""); string[] word = s.Split(" ".ToCharArray()); int ix = 0; string line = ""; while(ix < word.Length) { if((line + 1 + word[ix]).Length > maxlen) { line += del; res.Append(line); line = ""; } if(line.Length > 0) { line += " "; } line += word[ix]; ix++; } if(line.Length > 0) { line += del; res.Append(line); } return res.ToString(); } public static void Main(string[] args) { string s = "Lorem ipsum dolor sit amet, consectetuer adipiscing elit. In commodo. Mauris nec ipsum. Donec tincidunt. Aliquam erat volutpat. Pellentesque aliquam. Donec interdum malesuada arcu. Duis risus posuere."; Console.WriteLine(SplitLines(s, 20, "\r\n")); Console.WriteLine(SplitLines(s, 25, "
\r\n")); } } }