Q(uick)BASIC Function: INSTR
Quick View
INSTR
A string processing function that returns the character position of the first occurrence of a string in another string
Worth knowing
Useful and cross-version information about the programming environments of QBasic and QuickBasic.
Syntax
- INSTR([start%,]stringexpression1$,stringexpression2$)
Description/Parameter(s)
start% | Sets the character position where the search begins. If start% is omitted, INSTR starts at position 1. |
stringexpression1$ | The string to search. |
stringexpression2$ | The string to look for. |
Example
a$ = "Microsoft QBasic"
PRINT "String position ="; INSTR(1, a$, "QBasic")
See also:
Syntax
- INSTR([start,]stringexpression1,stringexpression2)
Description/Parameter(s)
Argument | Description |
start | An optional offset that sets the position for starting the search; start must be in the range 1-32,767. If start is not given, the INSTR function begins the search at the first character of stringexpression1. |
stringexpression1 | The string being searched. |
stringexpression2 | The string to look for. |
The arguments stringexpression1 and stringexpression2 can be string variables, string expressions, or string literals.
The value returned by INSTR depends on the following conditions:
Condition | Value Returned |
stringexpression2 found in stringexpression1 | The position at which the match is found |
start greater than length of stringexpression1 | 0 |
stringexpression1 is null string | 0 |
stringexpression2 cannot be found | 0 |
stringexpression2 is null string | start (if given); otherwise, 1 |
Use the LEN function to find the length of stringexpression1.
Example
This example uses INSTR and UCASE$ to determine a person's sex from their courtesy title (Mr., Mrs., or Ms.).
' Get a name.
CLS ' Clear screen
DO
INPUT "Enter name including courtesy title (Mr., Mrs., or Ms.): ", Nm$
LOOP UNTIL LEN(Nm$) >= 3
' Convert lowercase letters to uppercase.
Nm$ = UCASE$(Nm$)
' Look for MS, MRS, or MR to set Sex$.
IF INSTR(Nm$, "MS") > 0 OR INSTR(Nm$, "MRS") > 0 THEN
Sex$ = "F"
ELSEIF INSTR(Nm$, "MR") > 0 THEN
Sex$ = "M"
ELSE
' Can't determine sex, query user.
DO
INPUT "Enter sex (M/F): ", Sex$
Sex$ = UCASE$(Sex$)
LOOP WHILE Sex$ <> "M" AND Sex$ <> "F"
END IF
' Print result
PRINT "Sex is "; Sex$
Sample Output:
Enter name: Ms. Elspeth Brandtkeep Sex is F Enter name: Dr. Richard Science Enter sex (M/F): M Sex is MSyntax
- INSTR([start%,]stringexpression1$,stringexpression2$)
Description/Parameter(s)
- The arguments stringexpression1$ and stringexpression2$ can be string variables, string expressions, or string literals.
Usage Notes
- The value returned by INSTR depends on the following conditions:
Condition | Value returned |
stringexpression2$ found in stringexpression1$ | The position at which the match is found |
start% greater than length of stringexpression1$ | 0 |
stringexpression1$ is null string | 0 |
stringexpression2$ cannot be found | 0 |
stringexpression2$ is null string | start% (if given); otherwise, 1 |
- Use the LEN function to find the length of stringexpression1$.
Example
This example uses INSTR and UCASE$ to determine a person's gender from their courtesy title (Mr., Mrs., or Ms.).
'Get a name.
CLS 'Clear screen.
DO
INPUT "Enter name including courtesy title (Mr., Mrs., or Ms.): ", Nm$
LOOP UNTIL LEN(Nm$) >= 3
'Convert lowercase letters to uppercase.
Nm$ = UCASE$(Nm$)
'Look for MS, MRS, or MR to set Sex$.
IF INSTR(Nm$, "MS") > 0 OR INSTR(Nm$, "MRS") > 0 THEN
Sex$ = "F"
ELSEIF INSTR(Nm$, "MR") > 0 THEN
Sex$ = "M"
ELSE
'Can't determine sex, query user.
DO
INPUT "Enter sex (M/F): ", Sex$
Sex$ = UCASE$(Sex$)
LOOP WHILE Sex$ <> "M" AND Sex$ <> "F"
END IF
'Print result.
PRINT "Sex is "; Sex$
Sample Output:
Enter name: Ms. Elspeth Brandtkeep Sex is F Enter name: Dr. Richard Science Enter sex (M/F): M Sex is MSee also: