QB64 Phoenix Edition
Suggestion for new REPLACE$() function - Printable Version

+- QB64 Phoenix Edition (https://qb64phoenix.com/forum)
+-- Forum: QB64 Rising (https://qb64phoenix.com/forum/forumdisplay.php?fid=1)
+--- Forum: Code and Stuff (https://qb64phoenix.com/forum/forumdisplay.php?fid=3)
+---- Forum: Help Me! (https://qb64phoenix.com/forum/forumdisplay.php?fid=10)
+---- Thread: Suggestion for new REPLACE$() function (/showthread.php?tid=4413)



Suggestion for new REPLACE$() function - zaadstra - 01-25-2026

Lately I was writing a bit of code to do some job for my work.  Importing a clipboarded Excel column into an array, it came in as one large string.
Lazy as I am I thought to make ChatGPT do the dirty work   Smile  but as usual, it made a mess of the program.
It seems that ChatGPT doesn't know too much about QB64PE (just as Copilot as mentioned in another post), so in the end I gave up with it and provided the link of the wiki.  Which it immediately started scanning, so who knows, next time ....

As usual it was using non-existing commands.  But this time, it was a very interesting command:

_REPLACE$(source$,old$,new$)

Where in a source$, every occurrance of old$ was replaced with new$.
For a single replace, you could code this with a few commands, With multiple occurences, the code gets somewhat more complex, so here lies the real strength. Then I thought how nice it would be to have a fast, internal command.  There are many occasons where this is useful, like replacing the comma's for dots in a large number.  Now, I have a multi-line function for that.

Now I don't know if this is the right section of the forum, but I did not see a better place for the suggestion. If there is, please move it there!


RE: Suggestion for new REPLACE$() function - SMcNeill - 01-25-2026

https://qb64phoenix.com/forum/showthread.php?tid=3629  <-- There's a nice find and replace routine that does just that.  Wink


RE: Suggestion for new REPLACE$() function - SMcNeill - 01-25-2026

Slightly upgraded versions:

Code: (Select All)
Function String.Find& (Content As String, Search As String, CountTo As Long)
'If CountTo is 0, this routine will count the number of instances of the search term inside the content string
'If CountTo is >0, this routine will find the position of that instance, if it exists, inside the search string.
'For example, CountTo = 2, it'll find the location of the 2nd instance of the search term, skipping the first.
If CountTo < 0 Then Exit Function 'Can't find a negative position.
Dim As Long p, l, count
If Search$ = "" Then Exit Function
p = InStr(Content$, Search$)
l = Len(Search$)
Do While p& > 0
count = count + 1
If CountTo = count Then String.Find = p: Exit Function
p = InStr(p + l, Content$, Search$)
Loop
If CountTo = 0 Then String.Find = count
End Function

Function String.Replace$ (content$, from$, to$, position As Long)
Dim found As Long: found = String.Find(content$, from$, position)
If found Then String.Replace = Left$(content$, found - 1) + to$ + Mid$(content$, found + Len(from$))
End Function

Function String.Replace.All$ (content$, from$, to$)
'Inspired by the forum post here: https://qb64phoenix.com/forum/showthread...9#pid33559
'Original credit goes to mdijkens as I just tweaked it a little to make it a bit more library friendly
$Checking:Off
Dim As Long mp, pp, found, flen, tlen, p
Dim m As _MEM
Dim As String content2
found = String.Find(content$, from$, 0)
flen = Len(from$): tlen = Len(to$)

m = _MemNew(Len(content$) + (tlen& - flen&) * found): mp = 0: pp = 1
p = InStr(content$, from$)
Do While p > 0
_MemPut m, m.OFFSET + mp, Mid$(content$, pp, p& - pp): mp = mp + p& - pp
_MemPut m, m.OFFSET + mp, to$: mp = mp + tlen&: pp = p& + flen&
p& = InStr(p + flen&, content$, from$)
Loop
_MemPut m, m.OFFSET + mp, Mid$(content$, pp): mp = mp + Len(Mid$(content$, pp))
content2$ = Space$(mp): _MemGet m, m.OFFSET, content2$: _MemFree m
String.Replace.All$ = content2$
$Checking:On
End Function



RE: Suggestion for new REPLACE$() function - grymmjack - 01-26-2026

Throwing a hat in the ring for my STR.replace$ as well from QB64_GJ_LIB STRINGS library

FYI @zaadstra

Also, you are right the AIs don't know about a lot of QB64 stuff, so I wrote an MCP server for it:
QB64PE MCP Server

Cheers, and good luck.