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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Replace Function 1

Status
Not open for further replies.

arpan

Programmer
Oct 16, 2002
336
0
0
IN
An ASP page gets the following querystring:

iID1=73, 0, 0, 320, 402, 733

Based on this querystring, records are displayed from a database table to the users. Suppose a user clicks the record whose ID1=73. Then the page gets another querystring ID2=73 i.e.

iID2=73

The value of ID1 remains the same. Now I want to replace 73 (& not the 73 in 733) with 0. I am using the following Replace function to replace 73 with 0:

iIDs=Replace(iID1,iID2,"0")

But what is happening is both 73 & the 73 in 733 gets replaced with zero i.e. the querystring becomes

iID1=0, 0, 0, 320, 402, 03

Note that 733 has been replaced by 03 at the very end of the querystring since it substitutes the 73 in 733 also with a zero. But I want that it should be

iID1=0, 0, 0, 320, 402, 733

How do I ensure that only 73 gets replaced with zero & the 73 in 733 remains as it is? Please note that ID1 can be in any order. For e.g. it can be

iID1=733, 0, 0, 320, 402, 73

or

iID1=0, 733, 0, 73, 402, 320

or

iID1=320, 0, 73, 402, 733, 0

or

iID1=0, 0, 320, 73, 733, 402

Thanks,

Arpan
 
Hi...
I think the best way is to split the ID1 with "," and store the values in an array. then create a loop from lbound array to the ubout of the array and then check to see if the value in current pos. of the array is the same as ID2 then replace it with 0 and also in the loop, again create the ID1 query string :

MyArray = Split(ID1, ",", -1, 1)
ID1 = ""
For iCnt = LBound(MyArray, 1) To UBound(MyArray, 1)
If iCnt <> LBound(MyArray, 1) Then
ID1 = ID1 + &quot;,&quot;
End If

If MyArray(iCnt) = ID2 Then
ID1 = ID1 + &quot;0&quot;
Else
ID1 = ID1 + MyArray(iCnt)
End If
Next

----
TNX.
E.T.
 
Thanks, ehsant, for your help. Your code is working fine.

Regards,

Arpan
 
I'd also look at reg exps to do this - very good way of pattern matching / replacing.

These two threads might get you started: thread329-616385 and thread329-611378

Also this page is a very comprehensive overview of using them with VBscript:
if your stuck with them we can help you out.

Posting code? Wrap it with code tags: [ignore]
Code:
[/ignore][code]CodeHere
[ignore][/code][/ignore].
 
you can do it like this

replace(iId1 & &quot;, &quot;,iID2 & &quot;, &quot;,&quot;0&quot;)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top