extracting specific substrings from a string
| 8:30 pm on Jun 14, 2003 (gmt 0)|
| 1:48 am on Jun 15, 2003 (gmt 0)|
The key would be using the string.substring() method, which allows you to state beginning and ending character positions -- rather than using string.substr() which requires beginning position and length.
I'm not going to write the whole script for you, but here's how I see the logic for it:
1. assign a variable name to the string - s s.substring(m+1,n-1) - v
2. initialize a loop counter variable - i
3. set up an empty array to hold your extracted variables
----- BEGIN LOOP -----
4. find position of the first "special" character - m
5. ESCAPE LOOP if "special" character is not found
6. find position of the "stop" character - n
7. ESCAPE LOOP if "stop" character is not found
8. define a new variable to be
9. assign "v" to be a new array element (use the loop counter)
10. reassign the value of "s" to be s.substring(n+1,s.length)
11. increment the loop counter
----- LOOP BACK -----
That way you would create an array where each element was one of the variables you wanted to extract from the string.
| 2:21 am on Jun 15, 2003 (gmt 0)|
| 2:47 am on Jun 15, 2003 (gmt 0)|
Aren't we all ;)
Glad the logic helps out.
| 4:46 am on Jun 15, 2003 (gmt 0)|
var my_string = "hello this is a string";
var first = my_string.indexOf("o");
// Now "first" is set to 4, the position of the first "o" in the string.
// If it did not find the "o", it would return -1.
Then form a substring of my_string from first to my_string.length, and use indexOf() on the new string to find the occurance of the next character you are looking for. Then use substring to extract.
[added:] OOPS, I just realised this is not really an 'alternative', just more detail on how to do the steps 4 and 6 in Ted's post. However, another built-in that may be useful is:
string_name.split(separator char, limit)'