INSTR

From C64-Wiki
Jump to navigationJump to search
BASIC keyword
Keyword: INSTR
Abbreviation: IN Shift+S
Type: Function
Token code: UNK
Handling routine
in BASIC ROM:
UNK
List of all BASIC keywords


Remark: This article describes the BASIC function INSTR in in Commodore BASIC V3.5 or higher.

Type: INTEGER Function 
General Programming-Syntax: INSTR(<string 1>, <string 2>[,starting-position])

INSTR returns the position of string 2 within string 1 at or after the starting-position.

The starting-position defaults to the beginning of string 2. If no match is found, a value of 0 is returned.

Examples[edit | edit source]

S1$="SUPERCALIFRAGILISTICEXPIALIDOCIOUS"
S2$="FRAG"
PRINT INSTR(S1$,S2$)

returns 10

S1$="SUPERCALIFRAGILISTICEXPIALIDOCIOUS"
S2$="FRED"
PRINT INSTR(S1$,S2$)

returns 0

S1$="SUPERCALIFRAGILISTICEXPIALIDOCIOUS"
S2$="C"
PRINT INSTR(S1$,S2$)

returns 6

S1$="SUPERCALIFRAGILISTICEXPIALIDOCIOUS"
S2$="C"
PRINT INSTR(S1$,S2$,7)

returns 20

Selection menu with letter shortcut and elegant branching to the individual menu functions:

100 DO
110 :SCNCLR:PRINT SPC(10)"ARTICLE":PRINT
120 :PRINT SPC(5)"(N)EW"
130 :PRINT SPC(5)"(E)DIT OR SHOW"
140 :PRINT SPC(5)"(P)PRINTING LIST"
150 :PRINT:PRINT "WAHL?";
160 :DO
170 : GETKEY A$
180 : W=INSTR("NAD",A$)
190 :LOOP UNTIL W
200 :ON W GOSUB 1000,2000,3000
210 LOOP
...
1000 REM NEW ARTICLE
...
1999 RETURN
2000 REM ARTICLE CHANGE/EDIT
...
2999 RETURN
3000 REM LIST OF ARTICLES ON PRINTER
...
3999 RETURN

Determine commands for parsers of a programming language, e.g. for 6502-assembler-mnemonics:

100 REM INIT
110 MN$="ADCANDASLBCCBCSBEQBITBMIBNEBPLBRKBVCBVSCLC..."
111 REM MNEMONIC IS ALWAYS 3 CHARS LONG, AT POSITION 1, 4, 7, ...
...
1000 REM FOUND 6502 ASSEMBLER MNEMONIC IN I$
1001 REM RETOUR: B ... NO. OF COMMAND
1010 P=INSTR(MN$,I$)
1020 IF P = 0 OR INT((P-1)/3)*3 <> P-1 THEN P = -2: REM UNKNOWN
1030 B=(P+2)/3: REM NO. OF COMMAND >=1 OR 0 IF UNKNOWN
1040 RETURN


BASIC V2.0 (second release) Commands

ABS | AND | ASC | ATN | CHR$ | CLOSE | CLR | CMD | CONT | COS | DATA | DEF | DIM | END | EXP | FN | FOR | FRE | GET | GET# | GOSUB | GOTO | IF | INPUT | INPUT# | INT | LEFT$ | LEN | LET | LIST | LOAD | LOG | MID$ | NEW | NEXT | NOT | ON | OPEN | OR | PEEK | π | POKE | POS | PRINT | PRINT# | READ | REM | RESTORE | RETURN | RIGHT$ | RND | RUN | SAVE | SGN | SIN | SPC | SQR | STATUS/ST | STEP | STOP | STR$ | SYS | TAB | TAN | THEN | TIME/TI | TIME$/TI$ | TO | USR | VAL | VERIFY | WAIT