1

Is there a way assign split parts into struct?

I have strings that show times:

10:25, 10:55
11:05, 11:50
12:20, 13:10

I am iterating theses lines from text file and want to assign to ParkTime struct

public struct ParkTime
{
    public string startTime { get; set; }

    public string endTime { get; set; }
}

I tried to use LINQ but, first I need to validate lines using regular expressions.

4 Answers4

1

I suggest to use DateTime in your struct (or better a class but this is not important here). In this way the validation of the input is more focused to the expected value and you could avoid using regex expressions

List<ParkTime> parktimes = new List<ParkTime>();
foreach (string line in File.ReadLines("file.txt"))
{
    bool isValid = true;
    string[] times = line.Split(',');  
    DateTime dtInit;
    if(!DateTime.TryParseExact(times[0].Trim(), "HH:mm", 
        System.Globalization.CultureInfo.InvariantCulture,
        System.Globalization.DateTimeStyles.NoCurrentDateDefault,
        out dtInit))
        isValid = false;

    DateTime dtEnd;
    if (!DateTime.TryParseExact(times[1].Trim(), "HH:mm",
        System.Globalization.CultureInfo.InvariantCulture,
        System.Globalization.DateTimeStyles.NoCurrentDateDefault,
        out dtEnd))
        isValid = false;

    if (isValid)
        parktimes.Add(new ParkTime() { startTime = dtInit, endTime = dtEnd });
}

public struct ParkTime
{
    public DateTime startTime { get; set; }
    public DateTime endTime { get; set; }
}

And about using class or struct to model your data, this is a classical answer from E.Lippert (main developer for the C# Language)

Community
  • 1
  • 1
Steve
  • 213,761
  • 22
  • 232
  • 286
  • 1
    i wanted to give answer. btw you where faster. but because OP is reading text file its also good to mention using this. `foreach (var line in File.ReadLines("file.txt"))` – M.kazem Akhgary Dec 06 '15 at 13:43
  • @Steve Thanks. But it seems I need to format the times again when representing in view. –  Dec 06 '15 at 13:57
  • Sorry but I don't understand – Steve Dec 06 '15 at 13:58
  • Given that, I have 10:10, 11:10 line in text file. In view it' represented as 01.01.0001 0:00:00 - 01.01.0001 0:00:00 –  Dec 06 '15 at 13:59
  • 1
    In view you should apply the formatting required: es: _parktime.startTime.ToString("hh:mm");_ – Steve Dec 06 '15 at 14:03
  • Thanks! Works perfectly :) –  Dec 06 '15 at 17:20
  • Thanks for the suggestions, unfortunately I couldn't upvote your answer as I don't have enough reputation to do that. –  Dec 06 '15 at 18:42
0

You can use Linq Where to only select valid lines and then Select to create structs out of the lines.

lines
    .Where(line => validate(line))
    .Select(line => new ParkTime() {startTime = getStartTime(line), endTime = getEndTime(line));

Here are the methods used and their signature:

public bool validate(string line){
    return true; //validate if the line is correct
}

public string getStartTime(string line){
    return null; //select the start time from the line
}

public string getEndTime(string line){
    return null; //select the end time from the line
}
Domysee
  • 12,718
  • 10
  • 53
  • 84
0

For task like this, I use a simple TimeSlot struct which holds a timespan rather than an end time. Some operations are easier or more direct but, of course, that will depend on your planned usage for the parking hours.

public struct TimeSlot
{
    private DateTime _start;
    private TimeSpan _span;

    public DateTime Start
    {
        get
        {
            if (_start == null)
            {
                _start = DateTime.Today;
            }
            return _start;
        }

        set
        {
            _start = value;
        }
    }

    public TimeSpan Span
    {
        get
        {
            if (_span == null)
            {
                _span = new TimeSpan(0);
            }
            return _span;
        }

        set
        {
            if (value.Ticks >= 0)
            {
                _span = value;
            }
        }
    }

    public DateTime End
    {
        get
        {
            return Start.Add(Span);
        }
    }

    public TimeSlot(DateTime start, TimeSpan span)
    {
        _start = start;
        _span = span.Ticks >= 0 ? span : new TimeSpan(0);
    }
}
Gustav
  • 53,498
  • 7
  • 29
  • 55
0

If you know how ti iterate your lines you just need to split each line and then assing trimmed values to properties.

ParkTime pt = new ParkTime();

string[] arr = "10:25, 10:55".Split(",");

pt.startTime = arr[0].Trim();
pt.endTime = arr[1].Trim();
Ro.
  • 1,525
  • 1
  • 14
  • 17