04-18-2025, 06:04 PM
this is the function i use. see if you could figure out why.
otherwise yes, this too is flawed, but in a different way.
otherwise yes, this too is flawed, but in a different way.
Code: (Select All)
SUB ReplaceString3 (tx AS STRING, sfind AS STRING, repl AS STRING, numtimes AS _UNSIGNED LONG)
DIM AS STRING s, t, searc, replac
DIM AS _UNSIGNED LONG ls, count, u, j
DIM AS _BYTE goahead
IF (tx = "") OR (sfind = "") OR (sfind = repl) OR (LEN(sfind) > LEN(tx)) THEN EXIT SUB
FOR j = 1 TO 2
IF j = 1 THEN
searc = sfind
replac = CHR$(255)
ELSE
searc = CHR$(255)
replac = repl
END IF
s = UCASE$(searc)
t = UCASE$(tx)
ls = LEN(s)
count = 0
goahead = 1
DO
u = INSTR(t, s)
IF u > 0 THEN
tx = LEFT$(tx, u - 1) + replac + MID$(tx, u + ls)
t = UCASE$(tx)
IF numtimes > 0 THEN count = count + 1: IF count >= numtimes THEN goahead = 0
ELSE
goahead = 0
END IF
LOOP WHILE goahead
NEXT
END SUB