I need to split a text into its lines. But I also need to keep the line break characters at each line.
var text = "abc\r\ndef"; // should be two lines and 8 characters
// var text = "abc\rdef"; // should be two lines and 7 characters
// var text = "abc\ndef"; // should be two lines and 7 characters
var lines = Regex.Split(text, @"(?<=[\r\n|\r|\n])");
// I was hoping it would split into two lines:
// "abc\r\n"
// "def"
var countChars = 0;
foreach (var line in lines)
{
countChars += line.Length;
}
Assert.That(countChars, Is.EqualTo(8));
Assert.That(lines.Length, Is.EqualTo(2));
It did not feel so complicated in the beginning but I cannot make it work. Perhaps someone has a hint?