Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 91
  • Last Modified:

How to obtain string using substring and indexOf function

I have the following string
HELLO WORLD~~n~~  ~~n~~05DEC DEPARTURE DATE-----LAST DAY TO PURCHASE 06OCT/2359~~n~~       BASE FARE                 TAXES/FEES/CHARGES    TOTAL~~n~~ 1-    CAD12300                     12.20US       CAD284.68ADT~~n~~    XT      7.12CA      13.57XG      32.00SQ      12.00YQ ~~n~~          123.00                     60.68            284.68TTL~~n~~ADT

I want to obtain the value before TTL. As in 284.68, how do I do this using only substring and indexOf function?
0
badtz7229
Asked:
badtz7229
  • 6
  • 4
1 Solution
 
Julian HansenCommented:
This should do it
<p>F12 to view results in console</p>
<script>
var s = 'HELLO WORLD~~n~~  ~~n~~05DEC DEPARTURE DATE-----LAST DAY TO PURCHASE 06OCT/2359~~n~~       BASE FARE                 TAXES/FEES/CHARGES    TOTAL~~n~~ 1-    CAD12300                     12.20US       CAD284.68ADT~~n~~    XT      7.12CA      13.57XG      32.00SQ      12.00YQ ~~n~~          123.00                     60.68            284.68TTL~~n~~ADT';
var res = s.match(/(\d+\.\d\d)TTL/);
if (res) {
  console.log("Found: " + res[1]);
}
else {
  console.log('String not found');
}
</script>

Open in new window

0
 
badtz7229Author Commented:
@Julian,

this worked with your regexp.
however, there could be instances where the period may not be present or may be in a different location.  how can i account for that?
0
 
hieloCommented:
try:
var res = s.match(/\b(\d+(?:\.\d\d)?)TTL/);

Open in new window

0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
Julian HansenCommented:
<script>
var s = 'HELLO WORLD~~n~~  ~~n~~05DEC DEPARTURE DATE-----LAST DAY TO PURCHASE 06OCT/2359~~n~~       BASE FARE                 TAXES/FEES/CHARGES    TOTAL~~n~~ 1-    CAD12300                     12.20US       CAD284.68ADT~~n~~    XT      7.12CA      13.57XG      32.00SQ      12.00YQ ~~n~~          123.00                     60.68            284TTL~~n~~ADT';
var res = s.match(/(\d+[\.]?\d*)TTL/);
if (res) {
  console.log("Found: " + res[1]);
}
else {
  console.log('String not found');
}
</script>

Open in new window

0
 
Julian HansenCommented:
This might be more accurate in case you get .68TTL
var res = s.match(/(\d*[\.]?\d*)TTL/);
if (res) {
  console.log("Found: " + res[1]);
}
else {
  console.log('String not found');
}

Open in new window

0
 
badtz7229Author Commented:
thank you for your help that worked.
0
 
Julian HansenCommented:
You are welcome.
0
 
badtz7229Author Commented:
@julian, one quick question.
hope this finds you.
why are you getting res[1] and simply not res ?
0
 
Julian HansenCommented:
match returns an array if there were matches (null if there were not).
The first element in the array is the matched string the subsequent elements are any parenthesis-captured results i.e. matches of anything inside ( ) - which in this case is the number you are looking for.

You can see what this looks like by doing a console.log(res) right after the match to see what is returned.
0
 
badtz7229Author Commented:
thanks again.
0
 
Julian HansenCommented:
You are welcome.
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 6
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now