Forum Discussion

4m4d3u5's avatar
4m4d3u5
Contributor
6 years ago
Solved

Splitting strings

I have a string that is a time and a period that I want to parse; however, I want to parse this if the period is AM or PM. So the string for example with be: 8:40AM and I want to split that up into it's parts. What I want to do is split on the first letter so that it doesn't matter if it's am or pm so  something like:

 

var timePeriod = 8:40AM

var timePeriodParsedtimePeriod .split(Is letter)

var period = timePeriodParsed[1]

var hourMinutes = timePeriodParsed[0]

 

print(period)

output: AM

print(hoursMinutes)

output: 8:40

 

  • 4m4d3u5's avatar
    4m4d3u5
    6 years ago

    I am pretty sure I have to format it then I can do what I want. I have to do aqConvert.DateTimeToFormatStr(timePeriod, "%c"), then I can get the string 8:40 AM, then I can split by space, then split by :

5 Replies

    • 4m4d3u5's avatar
      4m4d3u5
      Contributor

      I am pretty sure I have to format it then I can do what I want. I have to do aqConvert.DateTimeToFormatStr(timePeriod, "%c"), then I can get the string 8:40 AM, then I can split by space, then split by :

      • Marsha_R's avatar
        Marsha_R
        Champion Level 3

         

        You said in your first post that you have a string.  If you don't actually have a string, then yes, you need to reformat it.  

         

        After that, you don't need to split anything.  

         

        Use Find to locate the :  and it will give you the position of : in the string 

        https://support.smartbear.com/testcomplete/docs/reference/program-objects/aqstring/find.html

         

        From there, the two characters before that are the hours and the two characters after that are the minutes.  Now you have the whole time  ColonPosition-2 through ColonPosition+2

         

        From your examples, it's not clear if you have a space before AM/PM or not.  

        If you do have a space, then AM/PM is at ColonPosition +4

        If you do not have a space, then AM/PM is at ColonPosition +3