Skip to content

Instantly share code, notes, and snippets.

@refactorsaurusrex
Created May 26, 2016 20:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save refactorsaurusrex/ddb065269f33d1cd5c4b96d9779f8338 to your computer and use it in GitHub Desktop.
Save refactorsaurusrex/ddb065269f33d1cd5c4b96d9779f8338 to your computer and use it in GitHub Desktop.
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;
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment