This might be a very simple question for you, but I’m stumped. I’m trying to read text file that has double-space as the delimiter.
"ColHdr1" "ColHdr2" "ColHdr3" "ColHdr4" "ColHDR5"
"fu" "bar" 1 2 3
"lorem" "ipsum" 5 6 7
I want to put all the lines into a list except the header line. This is the code that I have so far:
string prnFile = @"c:\temp\test.prn";
var fileLines = new List<string>();
foreach (var line in File.ReadAllLines(prnFile, Encoding.GetEncoding(1250)).Skip(1))
{
fileLines.Add(line.Split(new[] { " " },
StringSplitOptions.RemoveEmptyEntries));
}
In the end, I want the list to look like:
"fu" "bar" 1 2 3
"lorem" "ipsum" 5 6 7
Any idea what I’m doing wrong?
Your code looks okay to me at first sight, but I’d use LINQ all the way:
That will get you a
List<string[]>. It seems to work for me, but note that it won’t strip the double-quotes from the start/end of your strings.If you really want a flattened list, that’s remarkably easy – just change
SelecttoSelectMany:That gives you: