Avatar of lobos
lobos
 asked on

Parsing a string

I have a string in the following format and I need to parse it into 3 separate strings.
startTime = "2:15  PM"
to become into three strings

hour = 2
minute = 15
tod = PM

please advise...asap.
C#

Avatar of undefined
Last Comment
lobos

8/22/2022 - Mon
josgood

using System;
using System.Text.RegularExpressions;

class Demo {
     
   static void Main(string[] args) {
      string startTime = "2:15  PM";
      DateTime dt = DateTime.Parse(startTime);
      int hour = dt.Hour < 12 ? dt.Hour : dt.Hour - 12;
      Console.WriteLine(hour.ToString());
      Console.WriteLine(dt.Minute.ToString());
      Console.WriteLine(dt.Hour < 12 ? "AM" : "PM");
   }
}
Arkware

hour = startTime.Substring(0, startTime.IndexOf(":"));
minute = startTime.Substring(startTime.IndexOf(":") + 1, startTime.IndexOf(" ") - startTime.IndexOf(":"));
tod = startTime.Substring(startTime.IndexOf(" ") + 1);
lobos

ASKER
josgood, this is a web application...your syntax won't work for web..

arkware...error on line number 2

Specified argument was out of the range of valid values. Parameter name: 00
Your help has saved me hundreds of hours of internet surfing.
fblack61
ASKER CERTIFIED SOLUTION
Arkware

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
lobos

ASKER
Ok, great, I have upped the points to double because there is a second part to this question....now I need to parse the lunchtime variable as well...but this is more tricky....
see below of all potential values that I will need spilt off...below are all the possible scenarios that I would need to parse
lunchvalues = 15  min
lunchvalues = 30 min
lunchvalues = 45 min
lunchvalues = 1 hours
lunchvalues = 2 hours
one last one, which is the obvious of null

the above all the potential values and i need it spilt up for two variables

time = 45
amount = min

another example
time = 2
amount = hours

please advise...thanks.
lobos

ASKER
ok thanks for clarifying this...but now what do I do because I already had increased the points to this question on the premise of this second part question?
lobos

ASKER
25, then the second part was for another 25
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.