Ask a Question

Splitting strings


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]



output: AM


output: 8:40


Champion Level 2

The methods you want are here:


Look for the colon ":" and everything before that position is your hours

The next two positions after the colon are your minutes

The next two positions after the minutes are AM or PM or whatever

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 :

Champion Level 2


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


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

Yeah I guess that works too. I do have a string but Ican reformate it with the DateTimeToFormatStr so that I can a delimiter to use to parse the string which was the problem I had no delimiter.   I think it's clear but thanks. 

Esteemed Contributor

DateTimeToFormatStr is not for taking a string and formatting.  It is for taking a DateTime data type (a specific data type) and formatting it into a string.


If you have a string that you want to reformat in a particular way, then you'll need to do some manipulation of the string using the aqString object to split it up into different bits.

Robert Martin
[Hall of Fame]
Please consider giving a Kudo if I write good stuff

Why automate?  I do automated testing because there's only so much a human being can do and remain healthy.  Sleep is a requirement.  So, while people sleep, automation that I create does what I've described above in order to make sure that nothing gets past the final defense of the testing group.
I love good food, good books, good friends, and good fun.

Mysterious Gremlin Master
Vegas Thrill Rider
Extensions available
Showing results for 
Search instead for 
Did you mean: