Removing empty entries from an Array












0















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










share|improve this question


















  • 3





    There are many great libraries such as CSVHelper which will not only parse your file, but store it as typed data as an IEnumerable. No fumbling with arrays required

    – Make StackOverflow Good Again
    Nov 21 '18 at 18:02













  • Encapsulate your logic for detecting an empty line into a method called IsEmpty. then File.ReadAllLines(filename).Where(x => !IsEmpty(x)) will give you all non-empty lines which you can then process.

    – Will
    Nov 21 '18 at 18:04











  • @Disaffected1070452 I am dealing with both excel and csv files here. I had used EPPlus for excel but then that doesnt handles csv so I used this option. The only thing I can do if I dont want to use above code is to look at the file extension and if its excel use EPPlus else use csvhelper

    – user1563677
    Nov 21 '18 at 18:06











  • @Will sorry could you give me an example please.

    – user1563677
    Nov 21 '18 at 18:12











  • @Disaffected1070452 I just tried using CSVHelper but the same issue is there. I used this code: stackoverflow.com/questions/33294738/… the first answer in the above post. There also it reading the empty line and adding to result.

    – user1563677
    Nov 21 '18 at 18:23
















0















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










share|improve this question


















  • 3





    There are many great libraries such as CSVHelper which will not only parse your file, but store it as typed data as an IEnumerable. No fumbling with arrays required

    – Make StackOverflow Good Again
    Nov 21 '18 at 18:02













  • Encapsulate your logic for detecting an empty line into a method called IsEmpty. then File.ReadAllLines(filename).Where(x => !IsEmpty(x)) will give you all non-empty lines which you can then process.

    – Will
    Nov 21 '18 at 18:04











  • @Disaffected1070452 I am dealing with both excel and csv files here. I had used EPPlus for excel but then that doesnt handles csv so I used this option. The only thing I can do if I dont want to use above code is to look at the file extension and if its excel use EPPlus else use csvhelper

    – user1563677
    Nov 21 '18 at 18:06











  • @Will sorry could you give me an example please.

    – user1563677
    Nov 21 '18 at 18:12











  • @Disaffected1070452 I just tried using CSVHelper but the same issue is there. I used this code: stackoverflow.com/questions/33294738/… the first answer in the above post. There also it reading the empty line and adding to result.

    – user1563677
    Nov 21 '18 at 18:23














0












0








0








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










share|improve this question














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







c#






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 21 '18 at 18:00









user1563677user1563677

165218




165218








  • 3





    There are many great libraries such as CSVHelper which will not only parse your file, but store it as typed data as an IEnumerable. No fumbling with arrays required

    – Make StackOverflow Good Again
    Nov 21 '18 at 18:02













  • Encapsulate your logic for detecting an empty line into a method called IsEmpty. then File.ReadAllLines(filename).Where(x => !IsEmpty(x)) will give you all non-empty lines which you can then process.

    – Will
    Nov 21 '18 at 18:04











  • @Disaffected1070452 I am dealing with both excel and csv files here. I had used EPPlus for excel but then that doesnt handles csv so I used this option. The only thing I can do if I dont want to use above code is to look at the file extension and if its excel use EPPlus else use csvhelper

    – user1563677
    Nov 21 '18 at 18:06











  • @Will sorry could you give me an example please.

    – user1563677
    Nov 21 '18 at 18:12











  • @Disaffected1070452 I just tried using CSVHelper but the same issue is there. I used this code: stackoverflow.com/questions/33294738/… the first answer in the above post. There also it reading the empty line and adding to result.

    – user1563677
    Nov 21 '18 at 18:23














  • 3





    There are many great libraries such as CSVHelper which will not only parse your file, but store it as typed data as an IEnumerable. No fumbling with arrays required

    – Make StackOverflow Good Again
    Nov 21 '18 at 18:02













  • Encapsulate your logic for detecting an empty line into a method called IsEmpty. then File.ReadAllLines(filename).Where(x => !IsEmpty(x)) will give you all non-empty lines which you can then process.

    – Will
    Nov 21 '18 at 18:04











  • @Disaffected1070452 I am dealing with both excel and csv files here. I had used EPPlus for excel but then that doesnt handles csv so I used this option. The only thing I can do if I dont want to use above code is to look at the file extension and if its excel use EPPlus else use csvhelper

    – user1563677
    Nov 21 '18 at 18:06











  • @Will sorry could you give me an example please.

    – user1563677
    Nov 21 '18 at 18:12











  • @Disaffected1070452 I just tried using CSVHelper but the same issue is there. I used this code: stackoverflow.com/questions/33294738/… the first answer in the above post. There also it reading the empty line and adding to result.

    – user1563677
    Nov 21 '18 at 18:23








3




3





There are many great libraries such as CSVHelper which will not only parse your file, but store it as typed data as an IEnumerable. No fumbling with arrays required

– Make StackOverflow Good Again
Nov 21 '18 at 18:02







There are many great libraries such as CSVHelper which will not only parse your file, but store it as typed data as an IEnumerable. No fumbling with arrays required

– Make StackOverflow Good Again
Nov 21 '18 at 18:02















Encapsulate your logic for detecting an empty line into a method called IsEmpty. then File.ReadAllLines(filename).Where(x => !IsEmpty(x)) will give you all non-empty lines which you can then process.

– Will
Nov 21 '18 at 18:04





Encapsulate your logic for detecting an empty line into a method called IsEmpty. then File.ReadAllLines(filename).Where(x => !IsEmpty(x)) will give you all non-empty lines which you can then process.

– Will
Nov 21 '18 at 18:04













@Disaffected1070452 I am dealing with both excel and csv files here. I had used EPPlus for excel but then that doesnt handles csv so I used this option. The only thing I can do if I dont want to use above code is to look at the file extension and if its excel use EPPlus else use csvhelper

– user1563677
Nov 21 '18 at 18:06





@Disaffected1070452 I am dealing with both excel and csv files here. I had used EPPlus for excel but then that doesnt handles csv so I used this option. The only thing I can do if I dont want to use above code is to look at the file extension and if its excel use EPPlus else use csvhelper

– user1563677
Nov 21 '18 at 18:06













@Will sorry could you give me an example please.

– user1563677
Nov 21 '18 at 18:12





@Will sorry could you give me an example please.

– user1563677
Nov 21 '18 at 18:12













@Disaffected1070452 I just tried using CSVHelper but the same issue is there. I used this code: stackoverflow.com/questions/33294738/… the first answer in the above post. There also it reading the empty line and adding to result.

– user1563677
Nov 21 '18 at 18:23





@Disaffected1070452 I just tried using CSVHelper but the same issue is there. I used this code: stackoverflow.com/questions/33294738/… the first answer in the above post. There also it reading the empty line and adding to result.

– user1563677
Nov 21 '18 at 18:23












3 Answers
3






active

oldest

votes


















2














I advise using CSVHelper. CSV is not such a simple format as it seems.



With CSVHelper you can do this:



using (var csv = new CsvReader(reader))
{
csv.Configuration.SkipEmptyRecords = true;
var records = csv.GetRecords<Foo>().ToArray();
}





share|improve this answer
























  • there is no such property as SkipEmptyRecords

    – user1563677
    Nov 21 '18 at 18:35






  • 1





    Check that you use 2.x version. This field is in the documentation. joshclose.github.io/CsvHelper/2.x

    – Stanislav Molchanovsky
    Nov 21 '18 at 18:40













  • I have now modified to use CSVHelper. I was using their latest version and the following code was required: csv.Configuration.ShouldSkipRecord = record => { return record.All(string.IsNullOrEmpty); };

    – user1563677
    Nov 21 '18 at 20:11








  • 1





    If skipping records does not work because of empty strings, you can use TrimFields property

    – Stanislav Molchanovsky
    Nov 21 '18 at 20:24



















0














I'm almost certain the people advising you to CSVHelper or some other tool are correct, but if I were to do it by hand and wasn't absurdly concerned with performance, I'd do it like this:



    private void func()
{
string input = "123,a,b,3|456,c,d,5|111,acd,55,c1|,,,,|,,,,|,,,,";

List<IEnumerable<string>> parsedLines = new List<IEnumerable<string>>();
foreach (string line in input.TrimEnd('|').Split('|')) //foreach row
parsedLines.Add(line.Split(',')); //add that as a list of columns
//select rows that have at least one column with text
var result = parsedLines.Where(line => line.Any(field => !string.IsNullOrEmpty(field)));
}


If the goal is to throw away the empty lines BEFORE turning each into a collection of columns, this would work:



        string input = "123,a,b,3|456,c,d,5|111,acd,55,c1|,,,,|,,,,|,,,,";

var unparsedLines = input.TrimEnd('|').Split('|');
Regex re = new Regex(@"[^,s]", RegexOptions.Compiled); //search for any char that is not a comma or whitespace
var result = unparsedLines.Where(o => re.Match(o).Success);





share|improve this answer

































    0














    also



    s = ",,,|1,2,3|,,,,|,,,|4,56|,,,|,,|,,,,,";
    var sprev = s; string res;
    while(true)
    {
    var snew = Regex.Replace(sprev, "(\||^),{2,}(\||$)","|");
    if(snew == sprev)
    {
    res = snew.Trim('|');
    break;
    }
    sprev = snew;
    }





    share|improve this answer
























      Your Answer






      StackExchange.ifUsing("editor", function () {
      StackExchange.using("externalEditor", function () {
      StackExchange.using("snippets", function () {
      StackExchange.snippets.init();
      });
      });
      }, "code-snippets");

      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "1"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














      draft saved

      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53418053%2fremoving-empty-entries-from-an-array%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      3 Answers
      3






      active

      oldest

      votes








      3 Answers
      3






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      2














      I advise using CSVHelper. CSV is not such a simple format as it seems.



      With CSVHelper you can do this:



      using (var csv = new CsvReader(reader))
      {
      csv.Configuration.SkipEmptyRecords = true;
      var records = csv.GetRecords<Foo>().ToArray();
      }





      share|improve this answer
























      • there is no such property as SkipEmptyRecords

        – user1563677
        Nov 21 '18 at 18:35






      • 1





        Check that you use 2.x version. This field is in the documentation. joshclose.github.io/CsvHelper/2.x

        – Stanislav Molchanovsky
        Nov 21 '18 at 18:40













      • I have now modified to use CSVHelper. I was using their latest version and the following code was required: csv.Configuration.ShouldSkipRecord = record => { return record.All(string.IsNullOrEmpty); };

        – user1563677
        Nov 21 '18 at 20:11








      • 1





        If skipping records does not work because of empty strings, you can use TrimFields property

        – Stanislav Molchanovsky
        Nov 21 '18 at 20:24
















      2














      I advise using CSVHelper. CSV is not such a simple format as it seems.



      With CSVHelper you can do this:



      using (var csv = new CsvReader(reader))
      {
      csv.Configuration.SkipEmptyRecords = true;
      var records = csv.GetRecords<Foo>().ToArray();
      }





      share|improve this answer
























      • there is no such property as SkipEmptyRecords

        – user1563677
        Nov 21 '18 at 18:35






      • 1





        Check that you use 2.x version. This field is in the documentation. joshclose.github.io/CsvHelper/2.x

        – Stanislav Molchanovsky
        Nov 21 '18 at 18:40













      • I have now modified to use CSVHelper. I was using their latest version and the following code was required: csv.Configuration.ShouldSkipRecord = record => { return record.All(string.IsNullOrEmpty); };

        – user1563677
        Nov 21 '18 at 20:11








      • 1





        If skipping records does not work because of empty strings, you can use TrimFields property

        – Stanislav Molchanovsky
        Nov 21 '18 at 20:24














      2












      2








      2







      I advise using CSVHelper. CSV is not such a simple format as it seems.



      With CSVHelper you can do this:



      using (var csv = new CsvReader(reader))
      {
      csv.Configuration.SkipEmptyRecords = true;
      var records = csv.GetRecords<Foo>().ToArray();
      }





      share|improve this answer













      I advise using CSVHelper. CSV is not such a simple format as it seems.



      With CSVHelper you can do this:



      using (var csv = new CsvReader(reader))
      {
      csv.Configuration.SkipEmptyRecords = true;
      var records = csv.GetRecords<Foo>().ToArray();
      }






      share|improve this answer












      share|improve this answer



      share|improve this answer










      answered Nov 21 '18 at 18:28









      Stanislav MolchanovskyStanislav Molchanovsky

      467117




      467117













      • there is no such property as SkipEmptyRecords

        – user1563677
        Nov 21 '18 at 18:35






      • 1





        Check that you use 2.x version. This field is in the documentation. joshclose.github.io/CsvHelper/2.x

        – Stanislav Molchanovsky
        Nov 21 '18 at 18:40













      • I have now modified to use CSVHelper. I was using their latest version and the following code was required: csv.Configuration.ShouldSkipRecord = record => { return record.All(string.IsNullOrEmpty); };

        – user1563677
        Nov 21 '18 at 20:11








      • 1





        If skipping records does not work because of empty strings, you can use TrimFields property

        – Stanislav Molchanovsky
        Nov 21 '18 at 20:24



















      • there is no such property as SkipEmptyRecords

        – user1563677
        Nov 21 '18 at 18:35






      • 1





        Check that you use 2.x version. This field is in the documentation. joshclose.github.io/CsvHelper/2.x

        – Stanislav Molchanovsky
        Nov 21 '18 at 18:40













      • I have now modified to use CSVHelper. I was using their latest version and the following code was required: csv.Configuration.ShouldSkipRecord = record => { return record.All(string.IsNullOrEmpty); };

        – user1563677
        Nov 21 '18 at 20:11








      • 1





        If skipping records does not work because of empty strings, you can use TrimFields property

        – Stanislav Molchanovsky
        Nov 21 '18 at 20:24

















      there is no such property as SkipEmptyRecords

      – user1563677
      Nov 21 '18 at 18:35





      there is no such property as SkipEmptyRecords

      – user1563677
      Nov 21 '18 at 18:35




      1




      1





      Check that you use 2.x version. This field is in the documentation. joshclose.github.io/CsvHelper/2.x

      – Stanislav Molchanovsky
      Nov 21 '18 at 18:40







      Check that you use 2.x version. This field is in the documentation. joshclose.github.io/CsvHelper/2.x

      – Stanislav Molchanovsky
      Nov 21 '18 at 18:40















      I have now modified to use CSVHelper. I was using their latest version and the following code was required: csv.Configuration.ShouldSkipRecord = record => { return record.All(string.IsNullOrEmpty); };

      – user1563677
      Nov 21 '18 at 20:11







      I have now modified to use CSVHelper. I was using their latest version and the following code was required: csv.Configuration.ShouldSkipRecord = record => { return record.All(string.IsNullOrEmpty); };

      – user1563677
      Nov 21 '18 at 20:11






      1




      1





      If skipping records does not work because of empty strings, you can use TrimFields property

      – Stanislav Molchanovsky
      Nov 21 '18 at 20:24





      If skipping records does not work because of empty strings, you can use TrimFields property

      – Stanislav Molchanovsky
      Nov 21 '18 at 20:24













      0














      I'm almost certain the people advising you to CSVHelper or some other tool are correct, but if I were to do it by hand and wasn't absurdly concerned with performance, I'd do it like this:



          private void func()
      {
      string input = "123,a,b,3|456,c,d,5|111,acd,55,c1|,,,,|,,,,|,,,,";

      List<IEnumerable<string>> parsedLines = new List<IEnumerable<string>>();
      foreach (string line in input.TrimEnd('|').Split('|')) //foreach row
      parsedLines.Add(line.Split(',')); //add that as a list of columns
      //select rows that have at least one column with text
      var result = parsedLines.Where(line => line.Any(field => !string.IsNullOrEmpty(field)));
      }


      If the goal is to throw away the empty lines BEFORE turning each into a collection of columns, this would work:



              string input = "123,a,b,3|456,c,d,5|111,acd,55,c1|,,,,|,,,,|,,,,";

      var unparsedLines = input.TrimEnd('|').Split('|');
      Regex re = new Regex(@"[^,s]", RegexOptions.Compiled); //search for any char that is not a comma or whitespace
      var result = unparsedLines.Where(o => re.Match(o).Success);





      share|improve this answer






























        0














        I'm almost certain the people advising you to CSVHelper or some other tool are correct, but if I were to do it by hand and wasn't absurdly concerned with performance, I'd do it like this:



            private void func()
        {
        string input = "123,a,b,3|456,c,d,5|111,acd,55,c1|,,,,|,,,,|,,,,";

        List<IEnumerable<string>> parsedLines = new List<IEnumerable<string>>();
        foreach (string line in input.TrimEnd('|').Split('|')) //foreach row
        parsedLines.Add(line.Split(',')); //add that as a list of columns
        //select rows that have at least one column with text
        var result = parsedLines.Where(line => line.Any(field => !string.IsNullOrEmpty(field)));
        }


        If the goal is to throw away the empty lines BEFORE turning each into a collection of columns, this would work:



                string input = "123,a,b,3|456,c,d,5|111,acd,55,c1|,,,,|,,,,|,,,,";

        var unparsedLines = input.TrimEnd('|').Split('|');
        Regex re = new Regex(@"[^,s]", RegexOptions.Compiled); //search for any char that is not a comma or whitespace
        var result = unparsedLines.Where(o => re.Match(o).Success);





        share|improve this answer




























          0












          0








          0







          I'm almost certain the people advising you to CSVHelper or some other tool are correct, but if I were to do it by hand and wasn't absurdly concerned with performance, I'd do it like this:



              private void func()
          {
          string input = "123,a,b,3|456,c,d,5|111,acd,55,c1|,,,,|,,,,|,,,,";

          List<IEnumerable<string>> parsedLines = new List<IEnumerable<string>>();
          foreach (string line in input.TrimEnd('|').Split('|')) //foreach row
          parsedLines.Add(line.Split(',')); //add that as a list of columns
          //select rows that have at least one column with text
          var result = parsedLines.Where(line => line.Any(field => !string.IsNullOrEmpty(field)));
          }


          If the goal is to throw away the empty lines BEFORE turning each into a collection of columns, this would work:



                  string input = "123,a,b,3|456,c,d,5|111,acd,55,c1|,,,,|,,,,|,,,,";

          var unparsedLines = input.TrimEnd('|').Split('|');
          Regex re = new Regex(@"[^,s]", RegexOptions.Compiled); //search for any char that is not a comma or whitespace
          var result = unparsedLines.Where(o => re.Match(o).Success);





          share|improve this answer















          I'm almost certain the people advising you to CSVHelper or some other tool are correct, but if I were to do it by hand and wasn't absurdly concerned with performance, I'd do it like this:



              private void func()
          {
          string input = "123,a,b,3|456,c,d,5|111,acd,55,c1|,,,,|,,,,|,,,,";

          List<IEnumerable<string>> parsedLines = new List<IEnumerable<string>>();
          foreach (string line in input.TrimEnd('|').Split('|')) //foreach row
          parsedLines.Add(line.Split(',')); //add that as a list of columns
          //select rows that have at least one column with text
          var result = parsedLines.Where(line => line.Any(field => !string.IsNullOrEmpty(field)));
          }


          If the goal is to throw away the empty lines BEFORE turning each into a collection of columns, this would work:



                  string input = "123,a,b,3|456,c,d,5|111,acd,55,c1|,,,,|,,,,|,,,,";

          var unparsedLines = input.TrimEnd('|').Split('|');
          Regex re = new Regex(@"[^,s]", RegexOptions.Compiled); //search for any char that is not a comma or whitespace
          var result = unparsedLines.Where(o => re.Match(o).Success);






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 21 '18 at 19:10

























          answered Nov 21 '18 at 18:50









          zzxyzzzxyz

          2,2291725




          2,2291725























              0














              also



              s = ",,,|1,2,3|,,,,|,,,|4,56|,,,|,,|,,,,,";
              var sprev = s; string res;
              while(true)
              {
              var snew = Regex.Replace(sprev, "(\||^),{2,}(\||$)","|");
              if(snew == sprev)
              {
              res = snew.Trim('|');
              break;
              }
              sprev = snew;
              }





              share|improve this answer




























                0














                also



                s = ",,,|1,2,3|,,,,|,,,|4,56|,,,|,,|,,,,,";
                var sprev = s; string res;
                while(true)
                {
                var snew = Regex.Replace(sprev, "(\||^),{2,}(\||$)","|");
                if(snew == sprev)
                {
                res = snew.Trim('|');
                break;
                }
                sprev = snew;
                }





                share|improve this answer


























                  0












                  0








                  0







                  also



                  s = ",,,|1,2,3|,,,,|,,,|4,56|,,,|,,|,,,,,";
                  var sprev = s; string res;
                  while(true)
                  {
                  var snew = Regex.Replace(sprev, "(\||^),{2,}(\||$)","|");
                  if(snew == sprev)
                  {
                  res = snew.Trim('|');
                  break;
                  }
                  sprev = snew;
                  }





                  share|improve this answer













                  also



                  s = ",,,|1,2,3|,,,,|,,,|4,56|,,,|,,|,,,,,";
                  var sprev = s; string res;
                  while(true)
                  {
                  var snew = Regex.Replace(sprev, "(\||^),{2,}(\||$)","|");
                  if(snew == sprev)
                  {
                  res = snew.Trim('|');
                  break;
                  }
                  sprev = snew;
                  }






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 21 '18 at 20:27









                  AndrewFAndrewF

                  333




                  333






























                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53418053%2fremoving-empty-entries-from-an-array%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      鏡平學校

                      ꓛꓣだゔៀៅຸ໢ທຮ໕໒ ,ໂ'໥໓າ໼ឨឲ៵៭ៈゎゔit''䖳𥁄卿' ☨₤₨こゎもょの;ꜹꟚꞖꞵꟅꞛေၦေɯ,ɨɡ𛃵𛁹ޝ޳ޠ޾,ޤޒޯ޾𫝒𫠁သ𛅤チョ'サノބޘދ𛁐ᶿᶇᶀᶋᶠ㨑㽹⻮ꧬ꧹؍۩وَؠ㇕㇃㇪ ㇦㇋㇋ṜẰᵡᴠ 軌ᵕ搜۳ٰޗޮ޷ސޯ𫖾𫅀ल, ꙭ꙰ꚅꙁꚊꞻꝔ꟠Ꝭㄤﺟޱސꧨꧼ꧴ꧯꧽ꧲ꧯ'⽹⽭⾁⿞⼳⽋២៩ញណើꩯꩤ꩸ꩮᶻᶺᶧᶂ𫳲𫪭𬸄𫵰𬖩𬫣𬊉ၲ𛅬㕦䬺𫝌𫝼,,𫟖𫞽ហៅ஫㆔ాఆఅꙒꚞꙍ,Ꙟ꙱エ ,ポテ,フࢰࢯ𫟠𫞶 𫝤𫟠ﺕﹱﻜﻣ𪵕𪭸𪻆𪾩𫔷ġ,ŧآꞪ꟥,ꞔꝻ♚☹⛵𛀌ꬷꭞȄƁƪƬșƦǙǗdžƝǯǧⱦⱰꓕꓢႋ神 ဴ၀க௭எ௫ឫោ ' េㇷㇴㇼ神ㇸㇲㇽㇴㇼㇻㇸ'ㇸㇿㇸㇹㇰㆣꓚꓤ₡₧ ㄨㄟ㄂ㄖㄎ໗ツڒذ₶।ऩछएोञयूटक़कयँृी,冬'𛅢𛅥ㇱㇵㇶ𥄥𦒽𠣧𠊓𧢖𥞘𩔋цѰㄠſtʯʭɿʆʗʍʩɷɛ,əʏダヵㄐㄘR{gỚṖḺờṠṫảḙḭᴮᵏᴘᵀᵷᵕᴜᴏᵾq﮲ﲿﴽﭙ軌ﰬﶚﶧ﫲Ҝжюїкӈㇴffצּ﬘﭅﬈軌'ffistfflſtffतभफɳɰʊɲʎ𛁱𛁖𛁮𛀉 𛂯𛀞నఋŀŲ 𫟲𫠖𫞺ຆຆ ໹້໕໗ๆทԊꧢꧠ꧰ꓱ⿝⼑ŎḬẃẖỐẅ ,ờỰỈỗﮊDžȩꭏꭎꬻ꭮ꬿꭖꭥꭅ㇭神 ⾈ꓵꓑ⺄㄄ㄪㄙㄅㄇstA۵䞽ॶ𫞑𫝄㇉㇇゜軌𩜛𩳠Jﻺ‚Üမ႕ႌႊၐၸဓၞၞၡ៸wyvtᶎᶪᶹစဎ꣡꣰꣢꣤ٗ؋لㇳㇾㇻㇱ㆐㆔,,㆟Ⱶヤマފ޼ޝަݿݞݠݷݐ',ݘ,ݪݙݵ𬝉𬜁𫝨𫞘くせぉて¼óû×ó£…𛅑הㄙくԗԀ5606神45,神796'𪤻𫞧ꓐ㄁ㄘɥɺꓵꓲ3''7034׉ⱦⱠˆ“𫝋ȍ,ꩲ軌꩷ꩶꩧꩫఞ۔فڱێظペサ神ナᴦᵑ47 9238їﻂ䐊䔉㠸﬎ffiﬣ,לּᴷᴦᵛᵽ,ᴨᵤ ᵸᵥᴗᵈꚏꚉꚟ⻆rtǟƴ𬎎

                      Why https connections are so slow when debugging (stepping over) in Java?