Home > CBM > BASIC > Keywords > Mid | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
The keyword includes the '$' character.
~ or ~ MID$ ( target , start [ , length ] ) = subString
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.
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$.
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):
Examples (as command/statment):
© H2Obsession, 2014 |