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

replace last instance of a substring only 2

Status
Not open for further replies.

myatia

Programmer
Nov 21, 2002
232
What's the easiest and most efficient way to replace only the last instance of a substring? For example, how do I change the last instance of "dog" into "cat":

before = "My dog chased this dog and that dog."
after = "My dog chased this dog and that cat."

If anyone has any suggestions, I'd really appreciate them.

Thanks!

Misty
 
Something like

myString = "My dog chased this dog and that dog."

response.write Left(myString, InStrRev(myString ,"dog")-1) & Replace(myString ,"dog","cat",InStrRev(myString ,"dog"))


hth

simon
 
Try something like this:

myStr="My dog chased this dog and that dog."
targetStr="dog"
swapStr="cat"

ix=InStrRev(myStr,targetStr)
If ix>0 Then myStr=Left(myStr,ix-1) & Replace(Mid(myStr,ix),swapStr)



ASCII silly question, get a silly ANSI
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top