Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Parse String

Status
Not open for further replies.

timmbo

Programmer
Feb 22, 2001
167
US
Hi All,

I am trying to parse a string containing an account number and unitID. The string being passed looks like this.


%20%20%20%20%20%20%20%20%20%20%20%201014310995%28S1%295BSub%201%20Account%5D


I need to extract 1014310995 and S1 loading to their own variables.

Can someone give me any ideas how to do this?!

TIA,
Tim
 
This should do it. Someone may have a tidier way though. :)
Code:
source_string = unescape( "%20%20%20%20%20%20%20%20%20%20%20%201014310995%28S1%295BSub%201%20Account%5D" );
source_string = source_string.replace( /^(\s*)(\b[\w\W]*)$/, '$2' );

first_value = source_string.substring( 0, 10 );
second_value = source_string.substring( 11, 13 );

alert( first_value + "\n" + second_value ); // Just for testing.

HTH.
 
Well one thing you might want to do before
you start working with the string is strip
the special char's using unescape...

<script language=&quot;JavaScript&quot;>
var stra = '%20%20%20%20%20%20%20%20%20%20%20'+
'%201014310995%28S1%295BSub%201%20Account%5D';
function disp_unesc() {
alert(unescape(stra));
}
</script>

2b||!2b
 
Thanks very much theboyhope and Lrnmore! I'll give your suggestions a try.

Tim
 
I wrote this small script to display your string as an array:

Code:
<html>
<head>
<title>Blah</title>

<script language=&quot;JavaScript&quot;>

var arr = &quot;%20%20%20%20%20%20%20%20%20%20%20%201014310995%28S1%295BSub%201%20Account%5D&quot;

arr = nameArr.split(&quot;%&quot;);

document.write(arr.length + &quot;<br>&quot;);

for (i = 0; i < arr.length; i++) {
   document.write(i + &quot;: &quot; + arr[i] + &quot;<br>&quot;);
}

</script>

</head>
<body>
</body>
</html>

and this is the output it produced:

0:
1: 20
2: 20
3: 20
4: 20
5: 20
6: 20
7: 20
8: 20
9: 20
10: 20
11: 20
12: 201014310995
13: 28S1
14: 295BSub
15: 201
16: 20Account
17: 5D

Now..... provided that the string always has the same amount of %'s in it, you should be able to obtain the values you need from arr[12] and arr[13] simply by using the substring method. This should get you headed in the right direction, if you need help with the substring method just post another reply and I'll help ya out.

-kaht
 
My bad Kaht. I should have stated in my original qs. The % count can and will change. Any other suggestions would be most appreciated.

Tim
 
arr = nameArr.split(&quot;%&quot;);

should be

arr = arr.split(&quot;%&quot;);

sorry for the confusion
 
Where will the % count change? If it's just the ones at the beginning then the code I gave you at the start will still work. If it might change between the first and second values then it'll need tweaked a bit.

Let us know, eh. :)
 
I guess I wasn't clear on my last. What I meant was the number of %20 occurrances in the string can change. Hope this is a better explaination.

Tim
 
Are your parenthesis a constant??
We could use them with a split function to
seperate our data, you'll have to refine this
a bit but I think this might be a solution

20%201014310995%28S1%295BSub%201%20Account%5D

The unescape will strip them, then you could do
something like:

<script language=&quot;JavaScript&quot;>
var stra = '%20%20%20%20%20%20%20%20%20%20%20'+
'%201014310995%28S1%295BSub%201%20Account%5D';
function disp_unesc() {
alert(unescape(stra));
var strb = unescape(stra);
strc = strb.split(&quot;\(&quot;);
strb = strc[0].replace(/\s/g,&quot;&quot;);
strd = strc[1].split(&quot;\)&quot;);
stre = strd[0];
alert(strb +&quot;, &quot; +stre);
}
</script>

2b||!2b
 
Again, if it's just the ones (%20's) at the start of the string then the code I gave you earlier will still work. Otherwise, like I said, it'll need tweaked a wee bit.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top