// 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; namespace LineSplit { public class Program { public static void LineSplit(string s, int maxlinlen) { Console.WriteLine("".PadRight(maxlinlen, '*')); string[] parts = s.Split(' '); int ix = 0; string line = ""; while(ix < parts.Length) { if(parts[ix].Length > maxlinlen) { throw new Exception(string.Format("maxlinlen = {0} and word {1} does not work", maxlinlen, parts[ix])); } if(line.Length + 1 + parts[ix].Length > maxlinlen) { Console.WriteLine(line); line = ""; } line = line + " " + parts[ix]; ix++; } if(line.Length > 0) { Console.WriteLine(line); } Console.WriteLine("".PadRight(maxlinlen, '*')); } public static void Main(string[] args) { string s = "Jeg tester c#, fordi det er mandag. Og nu prøver vi tegn fremfor ord."; LineSplit(s, 40); LineSplit(s, 20); LineSplit(s, 10); } } }