MID$ function   

Purpose

Return a portion of a string.

Syntax

s$ = MID$(string_expression, start& [, length&])

Remarks

start& and length& are numeric variables or expressions.  As a function, MID$ returns a sub-string of string_expression that is length& characters long and starts at the start& character of string_expression.  For example:

a$ = MID$("PowerBASIC", 1, 2)

…returns "Po".  If length& is omitted, or there are fewer than length& characters to the right of the start& character of string_expression, all remaining characters of string_expression, including the start& character, are returned.  If start& is greater than the length of string_expression, MID$ returns an empty string. .  If length& is negative, it is interpreted as LEN(string_expression)-ABS(length&).  For example, MID$("1234567890",-3) returns "890".  Thus:

a$ = MID$("PowerBASIC", 4, 2)  ' returns "er"

a$ = MID$("PowerBASIC", 4)     ' returns "erBASIC"

a$ = MID$("PowerBASIC", 4, 20) ' returns "erBASIC"

a$ = MID$("PowerBASIC", 20)    ' returns a null string

a$ = MID$("1234567890",3,-4)   ' returns "345678"

If start& is negative, the starting position is assumed to be start& characters from the end of the string.  MID$("abcde", -3, 2) returns "cd", the function MID$("abcde", -3) returns "cde".

Restrictions

If start& evaluates to a position outside of the string on either side, or if start& is zero, an empty string is returned.  If length& is negative, all characters from start& position to the end of the string are returned.

See also

EXTRACT$, INSTR, LEFT$, LTRIM$, MID$ statementRIGHT$, RTRIM$, TALLY, TRIM$, VERIFY