QB64.com

QB64 is a modern extended BASIC programming language that retains QBasic/QuickBASIC 4.5 compatibility and compiles native binaries for Windows, Linux, and macOS.

The _INSTRREV function searches for a substring inside another string, but unlike INSTR it searches from right to left.

Syntax

position% = _INSTRREV([start%,] baseString$, subString$)

Parameter(s)

Description

Example(s)

Separating a file name from a full path.


fullPath$ = "C:\Documents and Settings\Administrator\Desktop\qb64\internal\c\libqb\os\win\libqb_1_2_000000000000.o"
file$ = MID$(fullPath$, _INSTRREV(fullPath$, "\") + 1)
PRINT file$


libqb_1_2_000000000000.o

Searching for multiple instances of a substring inside a base string, going from the end to the start.


sentence$ = " This is a string full of spaces, including at start and end... "
PRINT sentence$
DO
    findPrevSpace% = _INSTRREV(findPrevSpace% - 1, sentence$, SPACE$(1))
    IF findPrevSpace% = 0 THEN
        LOCATE 4, 1
        PRINT "No more spaces"
        EXIT DO
    END IF

    LOCATE 2, findPrevSpace%
    PRINT "^"
    totalSpaces = totalSpaces + 1

    IF findPrevSpace% = 1 THEN 
        LOCATE 4, 1
        PRINT "Last space found at position 1"
        EXIT DO
    END IF
LOOP
PRINT "Total spaces found: "; totalSpaces


 This is a string full of spaces, including at start and end... 
^    ^  ^ ^      ^    ^  ^       ^         ^  ^     ^   ^      ^

Last space found at position 1
Total spaces found: 13

See Also