Solved

XJS: extract string within a string

Posted on 2014-12-04
6
155 Views
Last Modified: 2014-12-05
I have the following variable
var Text = nodeRQ.ValueOfSelect("Text");

This contains
A/ONE/TWO/MAIN TEST EXTRA/2//15D/738-738//1488/2/USD/TAX/112/US/1600///ADT/NNN//ZA/1//T/ZZ/ZZ///01

how do i change this such that it reads as follows. where i removed 738-738
A/ONE/TWO/MAIN TEST EXTRA/2//15D///1488/2/USD/TAX/112/US/1600///ADT/NNN//ZA/1//T/ZZ/ZZ///01


so remove the contents between the 7th "/" and 8th "/" ?
Basically, using indexOf to find 7th "/" and 8th "/" then concatenate the remaining strings?
0
Comment
Question by:badtz7229
  • 3
  • 3
6 Comments
 
LVL 25

Accepted Solution

by:
Kyle Hamilton earned 500 total points
ID: 40481728
you could try something like this:

var s = "A/ONE/TWO/MAIN TEST EXTRA/2//15D/738-738//1488/2/USD/TAX/112/US/1600///ADT/NNN//ZA/1//T/ZZ/ZZ///01";
var arr = s.split("/");
arr.splice(7,1);
arr.join("/");

Open in new window


->
"A/ONE/TWO/MAIN TEST EXTRA/2//15D//1488/2/USD/TAX/112/US/1600///ADT/NNN//ZA/1//T/ZZ/ZZ///01"
0
 

Author Comment

by:badtz7229
ID: 40481816
@Kyle Hamilton - this seems to work. except the split function is replacing all the "/" with commas.
0
 
LVL 25

Assisted Solution

by:Kyle Hamilton
Kyle Hamilton earned 500 total points
ID: 40481829
no, the split function creates an array out of the string. you are seeing commas, because that is how an array is represented.

once you join the array back into a string, you will not see the commas anymore.

for clarity, you could change the line at the end to:

var newString = arr.join("/");

Open in new window


console.log(newString)

Open in new window


to see the new string.
0
Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

 

Author Comment

by:badtz7229
ID: 40482846
@Kyle Hamilton - that worked.
Only thing I just noticed is that when you remove the contents between the 7th "/" and 8th "/" , it's also removing the last "/". I want to maintain all "/"

so that it's

"A/ONE/TWO/MAIN TEST EXTRA/2//15D///1488/2/USD/TAX/112/US/1600///ADT/NNN//ZA/1//T/ZZ/ZZ///01"
0
 
LVL 25

Assisted Solution

by:Kyle Hamilton
Kyle Hamilton earned 500 total points
ID: 40482874
oh yeah. that's because we are removing the element. you can do this instead:

arr.splice(7,1, "");

add an empty element, to preserve the slash
0
 

Author Comment

by:badtz7229
ID: 40482971
yes that works. thanks so much
0

Featured Post

How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

Question has a verified solution.

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

Many times as a report developer I've been asked to display normalized data such as three rows with values Jack, Joe, and Bob as a single comma-separated string such as 'Jack, Joe, Bob', and vice versa.  Here's how to do it. 
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
In this tutorial viewers will learn how add a scalable full-width header using CSS3. Create a new HTML document with an internal stylesheet. Set a tiled background.:  Create a new div and name it Header. Position it with position:absolute at the top…
In this tutorial viewers will learn how to style elements, such a divs, with a "drop shadow" effect using the CSS box-shadow property Start with a normal styled element, such as a div.: In the element's style, type the box shadow property: "box-shad…

766 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