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 INSTR function searches for the first occurence of a search STRING within a base string and returns the position it was found.

Syntax

position% = INSTR([start%,] baseString$, searchString$)

Parameter(s)

Description

QBasic

Example(s)

Reading more than one instance of a word in a string using the INSTR return value as the start value plus 1.


text$ = "The cats and dogs where playing, even though dogs don't like cats."
DO
  findcats% = INSTR(findcats% + 1, text$, "cats") ' find another occurance after
  IF findcats% THEN PRINT "There is 'cats' in the string at position:"; findcats%
LOOP UNTIL findcats% = 0

findmonkey% = INSTR(text$, "monkeys")  ' find any occurance?
PRINT findmonkey%; "'monkeys' were found so it returned:"; findmonkey% 


There is 'cats' in the string at position: 5
There is 'cats' in the string at position: 62
 0 'monkeys' were found so INSTR returned: 0

Explanation: When the INSTR return value is 0 there are no more instances of a string in a string so the search loop is exited.

See Also