I am reading the contents of the CSV to a string as below:
string csvData = string.Empty;
using (var reader = new System.IO.StreamReader(file.OpenReadStream()))
using (ExcelPackage package = new ExcelPackage())
{
csvData = reader.ReadToEnd();
int totalLength = csvData.TrimEnd('|').Split('|').Length;
string[] result = null;
result = csvData.TrimEnd('|').Split('|');
if (String.IsNullOrEmpty(result[totalLength-1].Replace(",", "").Trim()))
{
result = result.Take(result.Count() - 1).ToArray();
}
//do some processing to the result here.
}
So below is the contents of my sample csvData:
123,a,b,3|456,c,d,5|111,acd,55,c1|,,,,
If you see the sample above it does contain the last empty row as it comes out from CSV. To remove the above empty row I use the above code that I have posted.
This all works fine. But the issue comes when I have more than one empty row as example below:
123,a,b,3|456,c,d,5|111,acd,55,c1|,,,,|,,,,|,,,,
With the above input my code just removes one empty row.
What I want that the result should have no empty rows as below:
123,a,b,3|456,c,d,5|111,acd,55,c1
How can I remove all the empty rows from my array.
Thanks