C # list count always returns 1, even when the list is empty

I am trying to debug a method in C #, but my basic syntax skills here seem to be missing! The method accepts the list of dates as a text string, separated by commas. This string is converted to a list and then processed. However, it seems that even when an empty string is passed to the method, it still outputs 1 when the list is counted.

The code is as follows:

public static int DaysLeft(DateTime endDate, DateTime startDate, Boolean excludeWeekends, String excludeDates) { int counter = 0; List<string> excludeDatesList = new List<string>(excludeDates.Split(',')); counter = excludeDatesList.Count; return counter; } 

If I pass an empty string as an excludeDates parameter, it returns 1. If I pass one date, it returns 1. If I pass two dates, it returns 2, etc. So this is the kind of work, except when nothing went in, when I expect it to return 0, but actually returns 1.

Can someone point me in the right direction?

thanks

+6
source share
9 answers

Even for an empty string, Split returns this string in the array, so the list will be created with ... one empty string creating a .Count of 1. [ Edit >: You can call excludeDates.Split(',', StringSplitOption.RemoveEmptyEntries) to this was not.]

In order for your function to behave as you expect, you should probably try to parse every string of "dates" returned from Split() , and only increment the counter by valid dates.

Something like that:

  int counter = 0; var possibleDates = excludeDates.Split(','); foreach (var dateStr in possibleDates) { // Right now it just counts "good" dates, though could also do something // with each date as well DateTime dt; if (DateTime.TryParse(dateStr, out dt)) counter++; } return counter; 

If you are looking for the easiest way, you should simply check the parameter to see if it is an empty string, in which case return 0:

 if (string.IsNullOrEmpty(excludeDates)) return 0; 
+10
source

Division by character returns an empty element.

Try using excludeDates.Split(',', StringSplitOptions.RemoveEmptyEntries) .

+8
source

You can use the option to delete empty entries.

 var blah = ""; var split = blah.Split(new[]{';'}, StringSplitOptions.RemoveEmptyEntries); var split2 = blah.Split(new[]{';'}); // Returns zero Console.WriteLine(split.Length); // Returns one Console.WriteLine(split2.Length); 
+3
source

This is normal behavior. When there is nothing to split, it will return a list with the string itself as the first element that counts as one.

+1
source

The array returned by String.Split () always has one element in it, even if it is an empty string.

+1
source

Inserting an empty string into the list will create a list of one item. This item will be your empty string.

+1
source

The result of calling string.Split in a string that does not contain a separator (in this case, a comma) is an array containing one element, namely the original string. This also happens if the line is empty.

The solution is to tell Split to omit empty entries:

 List<string> excluseDatesList = new List<string>(excludeDates.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries)); 
+1
source

As Dlev said, but to add an example to illustrate, if you pass the string "," is just a comma, then ",".Split(',').Count() will return 2. for ",,,," which is only four commas, you will return 5 for the account ...

0
source

if you look at the Split method, it has the following logic:

 int num = this.MakeSeparatorList(separator, ref sepList); if (num == 0 || count == 1) { return new string[] { this }; } 

Therefore, even if the string is an empty string, it returns one element in the array.

0
source

Source: https://habr.com/ru/post/893167/


All Articles