MID$ function

Purpose | Returns a part of a string.
Syntax | s$ = MID$(StringExpr, Start& [, Count&]) s$ = MID$(StringExpr, Start& TO End&)

Remarks

The MID$ function returns a part of a string expression. The first form tells the number of characters to extract, while the second form tells the start and end position instead. Both forms provide the same functionality, so the choice is just a matter of programmer convenience.

Start& and End& are positions in the string, starting with 1 as the first character. Count& tells the number of characters to extract. For example, both of the following examples return "wer".

a$ = MID$("PowerBASIC", 3, 3)

a$ = MID$("PowerBASIC", 3 TO 5)

If Count& is omitted, or there aren't enough characters in StringExpr, all remaining characters are returned. If there are no characters at the Start& position, an empty string is returned.

If Start& or End& are negative, the positions are counted backwards from the end of the string (-1 is the last character). If Count& is negative, it is interpreted as LEN(string_expression)-ABS(Count&).

See also | EXTRACT$, INSTR, LEFT$, LTRIM$, MID$ statement, RIGHT$, RTRIM$, SPLIT, TALLY, TRIM$, VERIFY

Example

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

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

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

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

a$ = MID$("abcde", -3, 2) ' returns "cd"

a$ = MID$("PowerBASIC", 4 TO 6) ' returns "erB"

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