The MID$
String Function
Another vital function is MID$
. This function selects a portion of any string it is given for its argument.
Type the command:
PRINT MID$("ABCOEFG",2,4)
The result shows you how MID$
works. In this case it displays a 4 character string, starting at the 2nd character of "ABCDEFG".
In formal terms, the MID$ function takes three arguments which are separated by commas and enclosed in brackets. The arguments are as follows:
- The first is the string to be used.
- The second is a number specifying the position of the first character in the result.
- The third is another number giving the length of the result.
As you would expect, any of the arguments can be variables of the appropriate sort. The length of the result can be anything from 0
(called the null
string) to the full length of the first argument. In practice it is often one character.
Here is a simple program to input a word and display it backwards. Study it carefully and note how the functions LEN
and MID$
are used:
10 INPUT "PLEASE TYPE A WORD"; X$
20 PRINT "YOUR WORD BACKWARD IS"
30 FOR J = LEN(X$) TO 1 STEP - 1
40 PRINT MID$(X$,J, 1);
50 NEXT J
60 STOP
Key the program in and check it for yourself; try out words of 1, 2 or more characters.