Removing empty entries from an Array
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#
|
show 1 more comment
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#
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
|
show 1 more comment
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#
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#
c#
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
|
show 1 more comment
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
|
show 1 more comment
3 Answers
3
active
oldest
votes
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();
}
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
add a comment |
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);
add a comment |
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;
}
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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();
}
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
add a comment |
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();
}
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
add a comment |
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();
}
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();
}
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
add a comment |
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
add a comment |
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);
add a comment |
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);
add a comment |
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);
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);
edited Nov 21 '18 at 19:10
answered Nov 21 '18 at 18:50
zzxyzzzxyz
2,2291725
2,2291725
add a comment |
add a comment |
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;
}
add a comment |
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;
}
add a comment |
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;
}
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;
}
answered Nov 21 '18 at 20:27
AndrewFAndrewF
333
333
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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