Reputation: 1368
I have a document that has several blank lines in the beginning. I am trying to remove the empty lines from the beginning of a document only. I have this code that removes blank lines from the entire document but I just want to remove the blank lines at the beginning. Typically, there are 1-4 blank lines prior to the information I need.
var lines = File.ReadAllLines(fileName).Where(arg => !string.IsNullOrWhiteSpace(arg));
File.WriteAllLines(fileName, lines);
I have considered using a while loop using while readline(fileName).First.Length = 0 but I'm worried that I might have to read and write very large files several times before I get the file I need (i.e. one without blank lines in the beginning).
I do want to get rid of line breaks.
Upvotes: 1
Views: 353
Reputation: 4833
var lines = File.ReadAllLines(fileName);
File.WriteAllLines(fileName, lines.SkipWhile(line => string.IsNullOrWhiteSpace(line)));
Upvotes: 4