locked
Splitting a String in Time Format RRS feed

  • Question

  • Hi,

           I have three strings

    string str1="8 AM to 10 AM";

    string str2="11 AM to 1 PM";

    string str3="5:30 PM to 7.30 PM";

    Now i want to split these strings into  following format

    string str1_ref1hours="8";

    string str1_ref2hours = "10";

    string str2_ref1hours="11";

    string str2_ref2hours="13";

    string str3_ref1hours="17";

    string str3_ref1minutes="30";

    string str3_ref2hours="19";

    string str3_ref2minutes="30";

    How can we get this done...


    Regards, Dillu


    • Edited by dillu Friday, April 5, 2013 10:33 AM
    Friday, April 5, 2013 10:13 AM

Answers

  • This solves all you test cases, excepts ":" or "." as separator and excludes out of range values:

                var input = "5:30 PM to 7.30 PM";
                const string TIMEPATTERN = @"([01]?[0-9])([:.]([0-5][0-9]))?\s*(AM|PM)";
                const string PATTERN = "^" + TIMEPATTERN + @"\s+to\s+" + TIMEPATTERN + "$";
    
                var mc = Regex.Matches(input, PATTERN, RegexOptions.IgnoreCase);
    
                if (mc.Count > 0) {
                   var h1 = int.Parse(mc[0].Groups[1].Value);
                   var m1 = int.Parse("0" + mc[0].Groups[3].Value);
                   var amPm1 = (mc[0].Groups[4].Value).ToUpper();
                   h1 = (amPm1 == "AM") ? h1 : h1 + 12;
    
                   var h2 = int.Parse(mc[0].Groups[5].Value);
                   var m2 = int.Parse("0" + mc[0].Groups[7].Value);
                   var amPm2 = (mc[0].Groups[8].Value).ToUpper();
                   h2 = (amPm2 == "AM") ? h2 : h2 + 12;
                }

    • Proposed as answer by Loesche Friday, April 5, 2013 12:02 PM
    • Marked as answer by dillu Friday, April 5, 2013 12:16 PM
    Friday, April 5, 2013 12:01 PM
  • Here is the code for outputing the results:

    static void Main(string[] args)
    {
    	string str1="8 AM to 10 AM";
     
    	string str2="11 AM to 1 PM";
     
    	string str3="5:30 PM to 11:30 PM";
    	Regex r = new Regex(@"(?<a>[0-9]+):?(?<a2>[0-9]*) [A|P|M]+ to (?<b>[0-9]+):?(?<b2>[0-9]*) [A|P|M]");
    	Match m = r.Match(str1);
    	writeOut(m);
    	m = r.Match(str2);
    	writeOut(m);
    	m = r.Match(str3);
    	writeOut(m);
    	Console.ReadLine();
    }
    static void writeOut(Match m)
    {
    	Console.WriteLine(m.Groups["a"]);
    	Console.WriteLine(m.Groups["a2"]);
    	Console.WriteLine(m.Groups["b"]);
    	Console.WriteLine(m.Groups["b2"]);
    }

    • Proposed as answer by FarnhamSurrey Friday, April 5, 2013 11:34 AM
    • Marked as answer by dillu Friday, April 5, 2013 12:20 PM
    Friday, April 5, 2013 11:27 AM

All replies

  • Your string str1_ref1hours is used twice...

    Lee Warren

    It would be greatly appreciated if you would mark any helpful entries as helpful and if the entry answers your question, please mark it with the Answer link.

    Friday, April 5, 2013 10:20 AM
  • This regex should do it

    Regex r = new Regex(@"(?<a>[0-9]+):?(?<a2>[0-9]*) [A|P|M]+ to (?<b>[0-9]+):?(?<b2>[0-9]*) [A|P|M]");

    Edit: I presume here that the time format uses a colon and not a period as you have in the last one
    Friday, April 5, 2013 11:21 AM
  • Here is the code for outputing the results:

    static void Main(string[] args)
    {
    	string str1="8 AM to 10 AM";
     
    	string str2="11 AM to 1 PM";
     
    	string str3="5:30 PM to 11:30 PM";
    	Regex r = new Regex(@"(?<a>[0-9]+):?(?<a2>[0-9]*) [A|P|M]+ to (?<b>[0-9]+):?(?<b2>[0-9]*) [A|P|M]");
    	Match m = r.Match(str1);
    	writeOut(m);
    	m = r.Match(str2);
    	writeOut(m);
    	m = r.Match(str3);
    	writeOut(m);
    	Console.ReadLine();
    }
    static void writeOut(Match m)
    {
    	Console.WriteLine(m.Groups["a"]);
    	Console.WriteLine(m.Groups["a2"]);
    	Console.WriteLine(m.Groups["b"]);
    	Console.WriteLine(m.Groups["b2"]);
    }

    • Proposed as answer by FarnhamSurrey Friday, April 5, 2013 11:34 AM
    • Marked as answer by dillu Friday, April 5, 2013 12:20 PM
    Friday, April 5, 2013 11:27 AM
  • string t = "10 AM to 11 AM";

     string[] ar = t.Split(new string[] { "to" }, 2, StringSplitOptions.None);

                            foreach (string m in ar)
                            {
                                MessageBox.Show(Convert.ToDateTime(m).Hour.ToString());
                            }

    • Proposed as answer by mbj79 Friday, April 5, 2013 11:52 AM
    • Unproposed as answer by mbj79 Friday, April 5, 2013 11:52 AM
    Friday, April 5, 2013 11:28 AM
  • Why do you want to get the parts as strings instead of integers?

    What you have is a string that contains two time specifications separated by the word "to". I would first split the string using " to " as a separator, and then use DateTime.Parse to convert each time specification to a DateTime.

    I haven't tried it, but letting the .NET framework do the parsing would be a very elegant solution, especially if you ever need to support other cultures/languages. (You would have to maintain your own list of translations of the word "to", though.)


    Marcus Björklund


    • Edited by mbj79 Friday, April 5, 2013 11:51 AM
    Friday, April 5, 2013 11:39 AM
  • This solves all you test cases, excepts ":" or "." as separator and excludes out of range values:

                var input = "5:30 PM to 7.30 PM";
                const string TIMEPATTERN = @"([01]?[0-9])([:.]([0-5][0-9]))?\s*(AM|PM)";
                const string PATTERN = "^" + TIMEPATTERN + @"\s+to\s+" + TIMEPATTERN + "$";
    
                var mc = Regex.Matches(input, PATTERN, RegexOptions.IgnoreCase);
    
                if (mc.Count > 0) {
                   var h1 = int.Parse(mc[0].Groups[1].Value);
                   var m1 = int.Parse("0" + mc[0].Groups[3].Value);
                   var amPm1 = (mc[0].Groups[4].Value).ToUpper();
                   h1 = (amPm1 == "AM") ? h1 : h1 + 12;
    
                   var h2 = int.Parse(mc[0].Groups[5].Value);
                   var m2 = int.Parse("0" + mc[0].Groups[7].Value);
                   var amPm2 = (mc[0].Groups[8].Value).ToUpper();
                   h2 = (amPm2 == "AM") ? h2 : h2 + 12;
                }

    • Proposed as answer by Loesche Friday, April 5, 2013 12:02 PM
    • Marked as answer by dillu Friday, April 5, 2013 12:16 PM
    Friday, April 5, 2013 12:01 PM
  • Great one that solved my problem

    Regards, Dillu

    Friday, April 5, 2013 12:18 PM
  • Its working fine when 

    string t = "10 AM to 11 AM";

    Its giving an exception when

    string t = "10 AM to 11.30 AM"; 


    Regards, Dillu

    Friday, April 5, 2013 12:21 PM