IonicWind Software

IWBasic => The Roundtable => Topic started by: LarryMc on December 30, 2011, 07:21:58 PM

Title: Remove and/or replace
Post by: LarryMc on December 30, 2011, 07:21:58 PM
While working on the new IDE's autoindent functionality I needed a routine to strip characters from a string.
The following subroutine is what I wound up with.

The source is the original string. Since strings are passed by reference I can modify it in the subroutine and the parent code can see the changes.
The srem is the character string to removed.  All occurrances of this string in the source will be removed.
casesen determines whether or not the search for srem in source is case sensitive
srpl is an optional string used to replace each occurance of srem.  They do not have to be the same length.

The include directive and Fletchie's ctl.lib is required because I used his XINSTR function for the case insensitive searches since INSTR is case sensitive.

If you use the subroutine make sure you size source,left, and right large enough to handle the max size of the possible results.

Hope someone can possiblity use it directly or as the basis for a better "mousetrap".

LarryMc


$include "ctl.inc"

sub REMOVE$(source as string, srem as string, casesen as int, opt srpl="" as string)
istring left[1000],right[1000]
int pos
if casesen
pos = INSTR(source,srem)
else
pos = XINSTR(source,srem)
endif
while pos>0
left =left$(source,pos-1)
right=mid$(source,pos+len(srem))
if srpl=""
source=left+right
else
source=left+srpl+right
endif
if casesen
pos = INSTR(source,srem)
else
pos = XINSTR(source,srem)
endif
endwhile
return
endsub