Solved

How to obtain string using substring and indexOf function

Posted on 2016-10-06
11
43 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 51

Expert Comment

by:Julian Hansen
Comment Utility
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
Comment Utility
@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
Comment Utility
try:
var res = s.match(/\b(\d+(?:\.\d\d)?)TTL/);

Open in new window

0
 
LVL 51

Expert Comment

by:Julian Hansen
Comment Utility
<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 51

Accepted Solution

by:
Julian Hansen earned 500 total points
Comment Utility
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
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 

Author Closing Comment

by:badtz7229
Comment Utility
thank you for your help that worked.
0
 
LVL 51

Expert Comment

by:Julian Hansen
Comment Utility
You are welcome.
0
 

Author Comment

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

Expert Comment

by:Julian Hansen
Comment Utility
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
Comment Utility
thanks again.
0
 
LVL 51

Expert Comment

by:Julian Hansen
Comment Utility
You are welcome.
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

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'…
This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
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…

771 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

7 Experts available now in Live!

Get 1:1 Help Now