refactorsaurusrex
5/26/2016 - 8:02 PM

A better way to add line endings at specific positions in a long text string.

A better way to add line endings at specific positions in a long text string.

var text = "A long string....";
var words = text.Split(' ');
var allLines = words.Skip(1).Aggregate(words.Take(1).ToList(), (lines, word) =>
{
    if (lines.Last().Length + word.Length >= 80)
        lines.Add(word);
    else
        lines[lines.Count - 1] += " " + word;
    return lines;
});