|
Post by davidk64 on Jun 12, 2022 11:14:04 GMT
So the MID$ help topic has a link to this function but alas a search of the forum shows it is only available in LB Does anybody have one written for use in JB by any chance to save me reinventing the wheel please? (It's a shame the project to provide feedback on the JB help went AWOL)
|
|
|
Post by plus on Jun 12, 2022 17:09:50 GMT
Here is a little help from QB64:
a$ = "this is a test and this is not a test and this might be a test." b$ = strReplace$(a$, "this", "Maybe THIS") print b$
Function strReplace$ (s$, replace$, new$) 'case sensitive QB64 2020-07-28 version If Len(s$) = 0 Or Len(replace$) = 0 Then strReplace$ = s$: Exit Function Else LR = Len(replace$): lNew = Len(new$) End If sCopy$ = s$ ' otherwise s$ would get changed in qb64 p = InStr(sCopy$, replace$) While p sCopy$ = Mid$(sCopy$, 1, p - 1) + new$ + Mid$(sCopy$, p + LR) p = InStr(sCopy$, replace$, p + lNew) ' InStr is differnt in JB Wend strReplace$ = sCopy$ End Function
It's a very handy function.
|
|
|
Post by davidk64 on Jun 13, 2022 10:17:24 GMT
Thanks +!
I really like the way the p variable works - it's a nifty piece of code I reckon. I certainly would never have thought to write the function that way.
|
|
|
Post by plus on Jun 13, 2022 15:40:45 GMT
Yes, it is also a great method for parsing strings into arrays (usu called Split, allot like Word$() only you do it once and for all time), just look for delimiters for replace$ string and instead of new$, use an oversized automatic global string array and track the top limit, last item number in the array.
|
|