MID$
Returns a number of characters from a given starting position of a string
Implemented by: altair8K, Applesoft, CBM, msatari, level2, trscolor, colormc, BBC, GWBASIC, bwbasic, bw32, ABasiC, QBasic, VBDOS, Gambas, minibasicmm, minibasicjs
With variations: atxl (position 0 causes error), trsdisk (can receive a string), level3 (can rceive a string), MSX (can be assigned a value, for substitution)
Also written as:
Usage
The basic syntax is:
stringvar$ = MID$ (str, startpos, n)
where str
is the source string, startpos
is the position of the first character of the desired substring (starting from left), and n
is the number of characters to extract.
The following example is from the Atari Microsoft BASIC manual:
100 A$ = "GETTHEMIDDLE" 110 PRINT MID(A$,4,3)
Variations
As expected, the numerical arguments to MID$ may vary a lot:
startpos
begins from 0 in msxstartpos
begins from 1 in msatari- Values for
startpos
andn
must be in the 8-bit range of 0 to 255 for most 8-bit BASICs. Strings could not have more than 255 characters in most of them anyway.
n
is optional and, if ommited, all characters from startpos
to the end of str
are returned − this stands true for msx.
If n
from startpos
surpasses the end of str
, most BASICs will simply return all characters from startpos
to the end of str
.
Watch out for differences in the effects of a negative value for n
or startpos
.
MID$= for substitutions
Some BASICs, especially Microsoft-based ones such as MSX BASIC and TRS-80 Level III BASIC, allow a strange syntax where MID$ receives a string value, which will replace the substring found by the function, effectively altering the source string str
. An example:
10 A$ = "ABC" 20 MID$(A$, 1, 1) = "Basic!" 30 PRINT A$
Examples
Comments
In the Microsoft realm, the keyword first appeared in the 8k version of Altair BASIC.