Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
String Find and Replace
#1
Code: (Select All)
$Console:Only
t$ = "ABCDEFGHIJKLMNOPQRSTUVWXYZAABVA"
Print Using "There are ## A's in "; String.Find(t$, "A", 0);
Print t$
Print "The first  B is at position "; String.Find(t$, "B", 1)
Print "The second B is at position "; String.Find(t$, "B", 2)
Print "The third  B is at position "; String.Find(t$, "B", 3)
Print
Print "Replace the A's with ' Hello World ', we get:"
t1$ = String.Replace(t$, "A", " Hello World ")
Print t1$


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
    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$)
    '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
    Dim m As _MEM
    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$ = content2$
    $Checking:On
End Function

Two routines in here which may be useful for folks:

String.Find will either tell you how many times something appears inside a string (Good for quick counting of said item.), or else it'll give the position of the Xth item inside that string.

String.Replace is a quick replacement routine which is based off the posts here: https://qb64phoenix.com/forum/showthread...9#pid33559  The main difference is that it properly sizes the return buffer so as to work easier in any library/plug in file.  It's a bit slower than the original, but safer and less memory intensive in most cases.
Reply


Messages In This Thread
String Find and Replace - by SMcNeill - 04-22-2025, 04:41 PM
RE: String Find and Replace - by SMcNeill - 04-24-2025, 07:19 AM
RE: String Find and Replace - by SMcNeill - 05-09-2025, 11:41 PM
RE: String Find and Replace - by bplus - 05-10-2025, 12:58 AM
RE: String Find and Replace - by bplus - 05-10-2025, 11:10 AM
RE: String Find and Replace - by SMcNeill - 05-10-2025, 12:02 PM
RE: String Find and Replace - by SpriggsySpriggs - 01-27-2026, 09:34 PM
RE: String Find and Replace - by SMcNeill - 01-27-2026, 09:51 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  String Math (Add and Subtract) SMcNeill 0 716 08-19-2022, 06:25 PM
Last Post: SMcNeill
  Variable length string database, using an index file SMcNeill 0 700 04-23-2022, 05:16 PM
Last Post: SMcNeill

Forum Jump:


Users browsing this thread: 1 Guest(s)