Share via


Razor - split string

Question

Wednesday, May 25, 2016 1:29 PM

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

All replies (1)

Wednesday, May 25, 2016 1:39 PM âś…Answered

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.