smr5
smr5

Reputation: 2793

Split string issues with null position

I have a string of the days of the week. I parse it in C# and store each day in a separate variable. For example:

string weekdayName = "Mon,Tue,Wed,Thu,Fri,Sat,Sun";

And the I split them using ',' as a delimiter:

var weekDayName = weekDay.Split(',');
var firstDay = weekDayName[0];
var secondDay = weekDayName[1];
var thirdDay = weekDayName[2];
var fourthDay = weekDayName[3];
var fifthDay =  weekDayName[4];
var sixDay = weekDayName[5];
var seventhDay = weekDayName[6];

Everything works. However, the string dynamically changes. A user assigns the days of the week. For example a string weekDayName could only contain "Mon,Tue". But the problem i'm running into is if not all the position contains value it will fail.

Index was outside the bounds of the array.

I have tried:

if (weekDayName[5].Length >0)
{
    var sixDay = weekDayName[5];
}

But it still fails...

How can I check and grab the value of the existing data, if some of the position are missing I just ignore them?

What I'm trying to achieve is:

DateTime currentDay = new DateTime();
currentDay = DateTime.Now;

if (currentDay.ToString("ddd") == firstDay || currentDay.ToString("ddd") == seconday)
{
    // I will check the currentDay against every day of the week
    //do something
}

Upvotes: 0

Views: 150

Answers (4)

foo-baar
foo-baar

Reputation: 1104

Wow that's a lot of code for a simple check, As mentioned by other members you can use LINQ to merge all validations in single query something like :

string weekdayName = "Mon,Tue,Wed,Thu,Fri,Sat,Sun";
var wc = weekdayName.Split(',');

if (wc.Any(str => str.Contains(DateTime.Today.DayOfWeek.ToString().Substring(0,3))))
{

}

Try this and let me know if you have any concerns.

Upvotes: 0

Leon Bouquiet
Leon Bouquiet

Reputation: 4382

If you're willing to try Linq (which is a language feature of C# 3.5 and higher, just include the System.Linq namespace) you could do it like this:

string[] names = weekdayName
    .Split(',')
    .Select(s => s.Trim())
    .Where(s => s.Length > 0)
    .ToArray();

The names array now contains all non-empty day names, without any leading or trailing spaces.

Upvotes: 0

SimonGA
SimonGA

Reputation: 94

There must be some smarter way of doing this, but since i do not know where the string originates from, it is hard for me to make a better solution.

However, given the current requirements, you could do something like:

string weekdayName = "Mon,Tue,Wed,Thu,Fri,Sat,Sun";

List<String> weekDaysList = weekdayName.Split(',').ToList();

foreach (var weekDay in weekDaysList.Take(2))
{
       if (weekDay == DateTime.Now.ToString("ddd"))
       {
              // do something
              break;
       }

 }

Upvotes: 0

TyCobb
TyCobb

Reputation: 9089

Your index out of range is not coming from where you think. I am of course assuming this since you tried to check length on an element that was never set if the user enters Mon,Tue.

var enteredDays = weekDay.Split(',');
for(var i = 0; i < enteredDays.Length; i++)
{
    var day = enteredDays[i];
    if(i == 0)
       first = day;
    else if(i == 1)
       second = day;

    ... etc....
}

Now you can check String.IsNullOrEmpty(..yourDay..) when you need to use the values you pulled from the array. You can't use .Length on them because they are null and will blow up unless you default them all to string.Empty.

Upvotes: 2

Related Questions