March 28, 2024, 03:54:50 PM

News:

IonicWind Snippit Manager 2.xx Released!  Install it on a memory stick and take it with you!  With or without IWBasic!


Remove and/or replace

Started by LarryMc, December 30, 2011, 07:21:58 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

LarryMc

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

LarryMc
Larry McCaughn :)
Author of IWB+, Custom Button Designer library, Custom Chart Designer library, Snippet Manager, IWGrid control library, LM_Image control library