Solved

How to obtain string using substring and indexOf function

Posted on 2016-10-06
11
76 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 57

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
How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

 
LVL 57

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 57

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
 

Author Closing Comment

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

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 57

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 57

Expert Comment

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

Featured Post

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

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.
This article discusses how to create an extensible mechanism for linked drop downs.
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…

734 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