[edit: 15 JAN 13:10]
Fixed function from editing the source$ after only partial success, now returns unedited source$ unless complete success.
Changed the "*" to "~" so that search and replace strings can include "*". Not a great solution but there's no reason to use "~" in DBC so it should be adequate.
[/edit]
I wanted to do some complex string replacement in one of my programs. The problem is I have an array ship#(x,15) that I want to get rid of but "x" is a different variable all over the place!
So I wrote this function that can take "~" as a search parameter that tells the function to include everything between the two terms either side of "~".
It should be useful for some people.
remstart
------------------
-=- String Replace -=-
by OBese87
15 JAN 2013
------------------
remend
a$ = "The_quick_brown_fox_jumps_over_the_lazy_dog."
print a$
print "Result: "; _replace$(a$,"quick~fox","cat")
print "Result: "; _replace$(a$,"quick~fox","nimble~cat")
print "Result: "; _replace$(a$,"quick~fox~","big~dog_chased_the_fox.")
print
b$ = "Ship#(j,1) * 5"
print _replace$(b$,"Ship#(~,1) *","shipx(~) /")
print _replace$(b$,"Ship#(~,2)","shipy(~)")
print _replace$(b$,"Ship#(~,3)","ship_owner(~)")
wait key
end
function _replace$(source$,search$,replace$)
sourcelen = len(source$)
searchlen = len(search$)
replacelen = len(replace$)
j=1
for i = 1 to sourcelen
if mid$(search$,j) = "~"
rem If last character of search$ is "~" then we ignore the rest of source$ and return here.
if j=searchlen
return$ = left$(replace$,replacelen)
if left$(search$,1)!"~" then return$ = left$(source$,i-j) + return$ :`drop the start of source$ if search$ starts with "~".
exitfunction return$
endif
rem if search$ first character is "~" then we don't do a subsearch.
if j > 1
`If there's a "~" in replace$ then we want to keep the letters in between the two
`search terms either side of the "~" in search$ ELSE we ignore them.
for k = 1 to replacelen-1
if mid$(replace$,k) = "~" then exit
next k
if k < replacelen
subsearch$ = right$(search$,searchlen-j)
dec k
else
subsearch$ = right$(search$,searchlen+1-j)
endif
rem Commence subsearch.
sub$ = _replace$( right$(source$,sourcelen+1-i), subsearch$, right$(replace$,replacelen-k-1) )
rem If subsearch fails then whole search fails.
rem Else whole search was successful.
if sub$ = right$(source$,sourcelen+1-i)
exitfunction source$
else
return$ = left$(replace$,k) + sub$
if left$(search$,1)!"~" then return$ = left$(source$,i-j) + return$ :`drop the start of source$ if search$ starts with "~".
exitfunction return$
endif
else
rem If search$(1) = "~" then we need to check the next character along against source$.
if mid$(search$,j+1) = mid$(source$,i) then inc j,2
endif
else
if mid$(search$,j) = mid$(source$,i)
if j >= searchlen
return$ = replace$ + right$(source$,sourcelen-i)
if left$(search$,1)!"~" then return$ = left$(source$,i-j) + return$ :`drop the start of source$ if search$ starts with "~".
exitfunction return$
endif
inc j
else
j=1
endif
endif
next i
rem If we get here the search failed so just return source$
endfunction source$
Shh... you're pretty.