INSTR

From C64-Wiki
Jump to navigationJump to search
BASIC keyword
Keyword: INSTR
Abbreviation: IN Shift+S
Type: Function
Token code: $d4 (212)
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, found on the Commodore Plus/4 and Commodore 128.

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 1. If no match is found, a value of 0 is returned.

It should be noted that other dialects of BASIC that support INSTR often have starting-position as the first parameter, not the last. This includes GW-BASIC and other common dialects.

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 V3.5 Commands

ABS | ASC | ATN | AUTO | BACKUP | BOX | CHAR | CHR$ | CIRCLE | CLOSE | CLR | CMD | COLLECT | COLOR | CONT | COPY | COS | DATA | DEC | DEF | DELETE | DIM | DIRECTORY | DLOAD | DO | DRAW | DS | DS$ | DSAVE | END | EL | ELSE | ER | ERR$ | EXIT | EXP | FN | FOR | FRE | GET | GET# | GETKEY | GOSUB | GOTO | GRAPHIC | GSHAPE | HEADER | HELP | HEX$ | IF | INPUT | INPUT# | INSTR | INT | JOY | KEY | LEFT$ | LEN | LET | LIST | LOAD | LOCATE | LOG | LOOP | MID$ | MONITOR | NEW | NEXT | ON | OPEN | PAINT | PEEK | POKE | POS | PRINT | PRINT USING | PRINT# | PUDEF | RCLR | RDOT | READ | REM | RENAME | RENUMBER | RESTORE | RESUME | RETURN | RGR | RIGHT$ | RLUM | RND | RUN | SAVE | SCALE | SCNCLR | SCRATCH | SGN | SIN | SOUND | SPC | SQR | SSHAPE | STOP | STR$ | SYS | TAB | TAN | TRAP | TROFF | TRON | UNTIL | USR | VAL | VERIFY | VOL | WHILE | WAIT