Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Suggestion for new REPLACE$() function
#1
Star 
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!
Reply
#2
https://qb64phoenix.com/forum/showthread.php?tid=3629  <-- There's a nice find and replace routine that does just that.  Wink
Reply
#3
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
Reply
#4
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.
grymmjack (gj!)
GitHubYouTube | Soundcloud | 16colo.rs
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Is there a menu function? Mad Axeman 17 1,019 12-17-2025, 04:43 AM
Last Post: SMcNeill
  SCREEN Function CletusSnow 8 803 11-16-2025, 05:09 PM
Last Post: TempodiBasic
  Replace routine Pete 3 742 10-31-2024, 04:46 PM
Last Post: Pete
  Using the Screen function PhilOfPerth 19 3,046 04-16-2024, 05:23 PM
Last Post: bobalooie
  Problem with one function Kernelpanic 3 852 08-29-2023, 11:26 PM
Last Post: Kernelpanic

Forum Jump:


Users browsing this thread: 1 Guest(s)