Removing empty entries from an Array











up vote
0
down vote

favorite












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
    – Disaffected 1070452
    2 days ago












  • 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
    2 days ago










  • @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
    2 days ago










  • @Will sorry could you give me an example please.
    – user1563677
    2 days ago










  • @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
    2 days ago















up vote
0
down vote

favorite












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
    – Disaffected 1070452
    2 days ago












  • 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
    2 days ago










  • @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
    2 days ago










  • @Will sorry could you give me an example please.
    – user1563677
    2 days ago










  • @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
    2 days ago













up vote
0
down vote

favorite









up vote
0
down vote

favorite











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 2 days ago









user1563677

147116




147116








  • 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
    – Disaffected 1070452
    2 days ago












  • 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
    2 days ago










  • @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
    2 days ago










  • @Will sorry could you give me an example please.
    – user1563677
    2 days ago










  • @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
    2 days ago














  • 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
    – Disaffected 1070452
    2 days ago












  • 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
    2 days ago










  • @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
    2 days ago










  • @Will sorry could you give me an example please.
    – user1563677
    2 days ago










  • @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
    2 days ago








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
– Disaffected 1070452
2 days ago






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
– Disaffected 1070452
2 days ago














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
2 days ago




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
2 days ago












@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
2 days ago




@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
2 days ago












@Will sorry could you give me an example please.
– user1563677
2 days ago




@Will sorry could you give me an example please.
– user1563677
2 days ago












@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
2 days ago




@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
2 days ago












3 Answers
3






active

oldest

votes

















up vote
2
down vote



accepted










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
    2 days ago






  • 1




    Check that you use 2.x version. This field is in the documentation. joshclose.github.io/CsvHelper/2.x
    – Stanislav Molchanovsky
    2 days ago












  • 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
    2 days ago








  • 1




    If skipping records does not work because of empty strings, you can use TrimFields property
    – Stanislav Molchanovsky
    2 days ago


















up vote
0
down vote













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






























    up vote
    0
    down vote













    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',
      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








      up vote
      2
      down vote



      accepted










      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
        2 days ago






      • 1




        Check that you use 2.x version. This field is in the documentation. joshclose.github.io/CsvHelper/2.x
        – Stanislav Molchanovsky
        2 days ago












      • 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
        2 days ago








      • 1




        If skipping records does not work because of empty strings, you can use TrimFields property
        – Stanislav Molchanovsky
        2 days ago















      up vote
      2
      down vote



      accepted










      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
        2 days ago






      • 1




        Check that you use 2.x version. This field is in the documentation. joshclose.github.io/CsvHelper/2.x
        – Stanislav Molchanovsky
        2 days ago












      • 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
        2 days ago








      • 1




        If skipping records does not work because of empty strings, you can use TrimFields property
        – Stanislav Molchanovsky
        2 days ago













      up vote
      2
      down vote



      accepted







      up vote
      2
      down vote



      accepted






      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 2 days ago









      Stanislav Molchanovsky

      447116




      447116












      • there is no such property as SkipEmptyRecords
        – user1563677
        2 days ago






      • 1




        Check that you use 2.x version. This field is in the documentation. joshclose.github.io/CsvHelper/2.x
        – Stanislav Molchanovsky
        2 days ago












      • 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
        2 days ago








      • 1




        If skipping records does not work because of empty strings, you can use TrimFields property
        – Stanislav Molchanovsky
        2 days ago


















      • there is no such property as SkipEmptyRecords
        – user1563677
        2 days ago






      • 1




        Check that you use 2.x version. This field is in the documentation. joshclose.github.io/CsvHelper/2.x
        – Stanislav Molchanovsky
        2 days ago












      • 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
        2 days ago








      • 1




        If skipping records does not work because of empty strings, you can use TrimFields property
        – Stanislav Molchanovsky
        2 days ago
















      there is no such property as SkipEmptyRecords
      – user1563677
      2 days ago




      there is no such property as SkipEmptyRecords
      – user1563677
      2 days ago




      1




      1




      Check that you use 2.x version. This field is in the documentation. joshclose.github.io/CsvHelper/2.x
      – Stanislav Molchanovsky
      2 days ago






      Check that you use 2.x version. This field is in the documentation. joshclose.github.io/CsvHelper/2.x
      – Stanislav Molchanovsky
      2 days ago














      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
      2 days ago






      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
      2 days ago






      1




      1




      If skipping records does not work because of empty strings, you can use TrimFields property
      – Stanislav Molchanovsky
      2 days ago




      If skipping records does not work because of empty strings, you can use TrimFields property
      – Stanislav Molchanovsky
      2 days ago












      up vote
      0
      down vote













      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



























        up vote
        0
        down vote













        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

























          up vote
          0
          down vote










          up vote
          0
          down vote









          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 2 days ago

























          answered 2 days ago









          zzxyz

          2,1551624




          2,1551624






















              up vote
              0
              down vote













              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

























                up vote
                0
                down vote













                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























                  up vote
                  0
                  down vote










                  up vote
                  0
                  down vote









                  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 2 days ago









                  AndrewF

                  332




                  332






























                       

                      draft saved


                      draft discarded



















































                       


                      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

                      Trompette piccolo

                      Slow SSRS Report in dynamic grouping and multiple parameters

                      Simon Yates (cyclisme)