I have a Stringbuilder object that has been populated from a text file.
How can I check the StringBuilder object for and remove consecutive “blank” lines.
i.e
Line 1: This is my text
Line 2:
Line 3: Another line after the 1st blank one
Line 4:
Line 5:
Line 6: Next line after 2 blank lines
(Line numbers given as reference only)
The blank line on Line 2 is fine, but I would like to remove the duplicate blank line, on Line 5, and so on.
If for argument sake Line 6 would have also been a blank line, and a Line 7 had a value, I would like Blank Line 5 and Blank Line 6 removed, so that there would only be 1 blank line between the Line 3 and Line 7.
Thanks in advance.
Do you have to already have the file contents in a StringBuilder?
It would be nicer to be able to read line-by-line. Something like:
Now you can read in your text (which has had dupe blank lines removed) like this:
Note: I’m only illustrating a technique here. There are other considerations: e.g. my iterator method holds the file open while the consumers are processing the foreach. This is nice and memory efficient (more so than reading into a string for example) as you are only dealing with one line at a time, but not ideal for files that take a long time to process.