?
Solved

How to obtain string using substring and indexOf function

Posted on 2016-10-06
11
Medium Priority
?
82 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 4
11 Comments
 
LVL 58

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
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

 
LVL 58

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 58

Accepted Solution

by:
Julian Hansen earned 2000 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
 

Author Closing Comment

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

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 58

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 58

Expert Comment

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

Featured Post

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

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

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.
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
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…
Suggested Courses

765 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