Commodore‎ > ‎BASIC‎ > ‎Keywords‎ > ‎

MID$

Keyword Abbreviation Token (hex) Version(s) Classification
MID$ M{Shift+I} CA 1.0 Command*, Function, or Statement*
The keyword includes the '$' character

 
  Syntax  
MID$ ( target , start [ , length ] )
 ~ or ~
MID$ ( target , start [ , length ] ) = subString
 
Parameters Type Legal Value(s) Default Value Note(s)
target String
any (0 to 255 chars)
start Unsigned Byte (form 1) 1 to 255
(form 2) ** 1 to LEN(target)
  ** start may be 0 to 255 if subString is empty
length Unsigned Byte 0 to 255  LEN(target)-start+1
or LEN(subString)
default gets the remainder (form 1)
default all characters of subString (form 2)
subString String 0 to LEN(target)-start+1 chars   Only a 'parameter' of form 2
 
Returns Type Value(s) Note(s)
subString String
0 to length characters
No value is returned by form 2 (but target is usually changed)
 
  Purpose  
As a function (form 1): return a subString of any length and at any position from a target string.
As a command or statement (form 2): replace character(s) inside of a target string.
 
  Remarks (general)  
If target is omitted or not a valid expression, SYNTAX ERROR occurs.  If target is not a string, TYPE MISMATCH ERROR occurs.
 
The start parameter is a 1-based position into the characters of the target string.  If start is omitted or not a valid expression, SYNTAX ERROR occurs.  If start is not numeric, TYPE MISMATCH ERROR occurs.  If start is not a legal value (see above) an ILLEGAL QUANTITY ERROR happens.
 
If length is not a valid expression, SYNTAX ERROR occurs.  If length is not numeric, TYPE MISMATCH ERROR occurs.  If length is not a legal value (see above) an ILLEGAL QUANTITY ERROR happens.
 
  Remarks (as Function)  
MID$ is one of the three string-splicing functions provided by BASIC (these complement string concatenation with the + operator).  MID$ is the most versatile one, it takes 3 parameters unlike the others (LEFT$ and RIGHT$).  MID$ returns a subString begging from the start position with (up to) length characters from the target string.
 
Since target must be a string type, you can use STR$ to convert a number into a string.
 
If start specifies a position beyond the end (i.e., more than the number of characters in target), an empty string is returned and there is no error.
 
Assuming start is less than or equal to the length of target (and at least 1), the remainder is a sub-string which begins at start and includes all characters to its right (until the end).  Otherwise the remainder is an empty string (assuming start is at least 1).  An error occurs (see above) is start is less than 1.
 
If length is omitted or specifies more characters than in the remainder, the entire remainder is returned and there is no error.  Note in this case, MID$ is very similar to RIGHT$, but you don't need to "count backwards" or know the length of the target string; BASIC does the work for you with MID$.
 
  Remarks (as Statement)  
*MID$ may be used a command or statement (form 2) only in version 3.5 and 7.0 of CBM BASIC.  Attempting this form in other versions of BASIC (in particular, 4.x) will generate SYNTAX ERROR.  This form replaces length characters beginning at the start position inside the target string with the first length characters of subString (default all characters of subString).
 
The target must be a string variable; attempting to use a string expression (including a literal string value) will generate SYNTAX ERROR.  Attempting to use most reserved variables will also cause SYNTAX ERROR.  There is a bug when used with TI$, which is the one reserved variable the user is allowed to change.  A MID$ command/statement using TI$ as the target will generate ILLEGAL QUANTITY ERROR, even when the same command/statement works fine with any other 6-character string.
 
MID$ never changes the length of target.  Because of this, start + LEN(subString) must not be greater than LEN(target) or an ILLEGAL QUANTITY ERROR is generated.  The one exception is if subString is an empty string; in this case, start may be any number from  0 to 255.
 
Because start must be at least 1 but not more than the length of target, this form of MID$ will never work with an empty-string target (which has a length of 0)... unless the subString is also an empty string (which never changes target).
 
If length is omitted or specifies more characters than in subString, the entire subString will used.  This is normally okay, but if the length of subString or the specified length (whichever is shorter) plus start is greater than the length of target then ILLEGAL QUANTITY ERROR occurs.
 
As long as you don't need to change the length of target, this is a pretty powerfull command/statement which replaces a combination of LEFT$, + operator(s), and MID$ or RIGHT$ and possibly LEN which would otherwise be needed.
 
Examples (as function):
PRINT MID$(100.8, 2)

?TYPE MISMATCH ERROR 
READY.
PRINT MID$("100.8", 2)
00.8
 
READY.
PRINT MID$("HELLO", 2)
ELLO

READY.
PRINT RIGHT$("HELLO", 16)


READY.
PRINT RIGHT$("HELLO", 2, 3)
ELL

READY.
PRINT RIGHT$("HELLO", 2, 16)
ELLO

READY.
 
Examples (as command/statment):
MID$(100.8, 2) = "K"

?SYNTAX ERROR 
READY.
MID$("100.8", 2) = "K"

?SYNTAX ERROR 
READY.
A$ = "HELLO" : PRINT A$
HELLO

READY.
MID$(A$, 1)="C" : PRINT A$
CELLO

READY.
MID$(A$, 2)="ARG" : PRINT A$
CARGO

READY.
MID$(A$, 4, 2)="ARG" : PRINT A$
CARAR

READY.
MID$(A$, 4)="ARG" : PRINT A$

?ILLEGAL QUANTITY ERROR
READY.
 
 
  Compare With  
 
  Contrast With  
 
  See Also  

© H2Obsession, 2014
Comments