locked
Razor - split string RRS feed

  • Question

  • User-173651909 posted

    I'm writing an MVC 5 app and I need to split a string pulled from a database. The string stored in the database is "opening times" and is stored as something like "09:00 - 17:30". In razor, I need to split out the "09:00" and the "17:30" removing the " - " from the middle. I had the following but get an error:

    string[] times = (Model.HoursOfOperation).Split(" - ");

    This tells me it cannot convert string to char. Any thoughts?

    Thanks in advance
    Adam

    Wednesday, May 25, 2016 1:29 PM

Answers

  • User475983607 posted

    Use a split with a char like this.

    string[] times = (Model.HoursOfOperation).Split('-')

    Replace the the spaces with an empty string like this...

    string[] times = (Model.HoursOfOperation).Replace(" ", "").Split('-');

    Or trim the string when they are used.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, May 25, 2016 1:39 PM