QB64 Phoenix Edition

Full Version: Reverse search and case-insernsitive search routines
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
i had a need of a routine to do the opposite of INSTR( in which I wanted to find the last appearance of a search field in the target string. So I wrote the following functions:

  1. '  Reverse instr: finds last position of search in target
    Function Rinstr& (Target As String, Search As String)
  2. ' Case insensitive search
    Function Iinstr& (start As Long, Target As String, Search As String)
  3. ' Case insensitive reverse seaarch
    Function RIinstr& (Target As String, Search As String)

I'm not sure how useful this would be, but I suspect someone is going to need one of these at some point.

The functions include a program that runs a set of tests to confirm they work.
Glad to see I'm not the only one who is Wikilliterate around here. Anyway, kudos for writing your own version. I dd the same several years back, before _INSTRREV came along. It's a big help for word wrap in WP apps. I had a bit of a go-around with Fell when _INSTRREV first came out, regarding the seed parameter. _INSTRREV keeps the same forward seed counting as INSTR. There are reasons for that, which slip my mind at the moment, but I do recall I made an easy workaround, so it could seed backwards. Compare these two...

Code: (Select All)
For i = 1 To 8
    Print i; _InStrRev(i, "12*456*8", "*")
Next

For i = 1 To 8
    Print i; _InStrRev(Len("12*456*8") - i + 1, "12*456*8", "*")
Next

Pete
@Pete The reason is for easy splitting of the string.

a$ = "C:\My Dir\My File.txt"

Now, get the last slash in that string, to break it down to path + file:

path$ = LEFT$(a$, _INSTRREV(a$, "\"))
file$ = MID$(a$, _INSTRREV(a$, "\") + 1)
But your example is SEEDLESS. Great if you're growing grapes, but let's not wine about it...

Code: (Select All)
'        1111111111222222222233333333
'1234567890123456789012345678901234567
'c:\root\mystuff\basic\qb64\myfile.bas
file$ = "c:\root\mystuff\basic\qb64\myfile.bas"
seed% = 1
Do
    next_to_last$ = Mid$(file$, _InStrRev(Len(file$) - seed%, file$, "\") + 1)
    Print seed%, next_to_last$
    seed% = Len(next_to_last$) + 1
Loop Until seed% > Len(file$)
Print
seed% = Len(file$)
Do
    next_to_last$ = Mid$(file$, _InStrRev(seed%, file$, "\") + 1)
    Print seed%, next_to_last$
    seed% = _InStrRev(seed%, file$, "\") - 1
Loop Until seed% < 0
End

So a couple of chop methods, but my initial contention was when we count in reverse, we should seed in reverse. I let that go, because there is at least a since of continuity that even though the searching part of the string has been reversed, the seeding order, from beginning to end, remains constant.

Pete
(04-04-2024, 12:38 AM)SMcNeill Wrote: [ -> ]https://qb64phoenix.com/qb64wiki/index.php/INSTRREV

Hadn't noticed this one! Could be quite useful for some of my little progs.  Smile
Here are some custom Instr functions.

The first as a string comparison, the second a filename comparison.

The string one allows * and ? where the second one has *, ?, ^ comparison.

Erik.