Search code examples
c#arraysparsingsplitline-breaks

How to split a string by line breaks and not lose multiple line breaks in a row?


I have the following code that I use to take a string and break it up by line breaks:

var delimiters = new string[] { "\\v", "\v", "\r", "\n" };
string[] split = textWithStyle.Text.Split(
                     delimiters, 
                     StringSplitOptions.RemoveEmptyEntries);

I then loop through the split array to render. So if my string is:

Today is Monday and the 7th
Tomorrow is Tuesday and the 8th

I get an array with 2 items:

[0] Today is Monday and the 7th
[1] Tomorrow is Tuesday and the 8th

The problem i just realized is that if the string has multiple line breaks in a row like:

Today is Monday and the 7th


Tomorrow is Tuesday and the 8th

if i look in a text editor, i see multiple CRLFs in a row here but my parsing code doesn't differentiate this use case from a single line break and the above will still only create 2 elements in the array with the individual lines

How can i change my parsing code so if i have multiple line breaks in a row, it add every line breaks except the first one into the array. So if the above string has 3 CRLFs then i would want my array to be:

[0] Today is Monday and the 7th
[1] empty string
[2] empty string
[3] Tomorrow is Tuesday and the 8th

If I simply remove the StringSplitOptions.RemoveEmptyEntries, then i wind up getting

[0] Today is Monday and the 7th
[1] empty string
[2] empty string
[3] empty string
[4] empty string
[5] Tomorrow is Tuesday and the 8th

which i don't want (as it has more space entries than i want)


Solution

  • Remove StringSplitOptions.RemoveEmptyEntries and remove some of the entries and just leave:

     var delimiters = new string[] { "\\v", "\v", "\r\n" }; 
     string[] split = textWithStyle.Text.Split( delimiters); 
    

    For each empty entry in your resulting array, that is a line break.