Solved

How to obtain string using substring and indexOf function

Posted on 2016-10-06
11
50 Views
Last Modified: 2016-10-07
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
Comment
Question by:badtz7229
  • 6
  • 4
11 Comments
 
LVL 52

Expert Comment

by:Julian Hansen
ID: 41832782
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
 

Author Comment

by:badtz7229
ID: 41832809
@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
 
LVL 82

Expert Comment

by:hielo
ID: 41833021
try:
var res = s.match(/\b(\d+(?:\.\d\d)?)TTL/);

Open in new window

0
 
LVL 52

Expert Comment

by:Julian Hansen
ID: 41833127
<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
 
LVL 52

Accepted Solution

by:
Julian Hansen earned 500 total points
ID: 41833138
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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 

Author Closing Comment

by:badtz7229
ID: 41833612
thank you for your help that worked.
0
 
LVL 52

Expert Comment

by:Julian Hansen
ID: 41833702
You are welcome.
0
 

Author Comment

by:badtz7229
ID: 41833742
@julian, one quick question.
hope this finds you.
why are you getting res[1] and simply not res ?
0
 
LVL 52

Expert Comment

by:Julian Hansen
ID: 41833789
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
 

Author Comment

by:badtz7229
ID: 41833792
thanks again.
0
 
LVL 52

Expert Comment

by:Julian Hansen
ID: 41833826
You are welcome.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In Part 1 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7849-Hex-Maze.html) we covered the hexagonal maze basics -- how the cells are represented in a JavaScript array and how the maze is displayed.  In this part, we'…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

929 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now