MID$ statement

Purpose | Replace characters in a string variable.
Syntax | MID$(StringVar, Start& [, Count&]) = replacement MID$(StringVar, Start& TO End& = replacement

Remarks

The MID$ statement replaces characters in a string variable. The first form tells the number of characters to replace, 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 replace.

If Count& is omitted, or there aren't enough characters in StringVar, all remaining characters are replaced. If there are no characters at the Start& position, no operation is performed.

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&).

The replacement will never extend past the end of StringVar. In other words, MID$ cannot alter the length of a string.

Restrictions | If Start& evaluates to a position outside of the string on either side, or if Start& is zero, no operation is performed.
See also | BUILD$, INSTR, LTRIM$, MID$ function, REMOVE$, REPLACE, RTRIM$, TALLY, TRIM$, VERIFY
Example | DummyString$ = "1234567890" FOR M = 1 TO 10 TestString$ = DummyString$ MID$(TestString$,1,M) = "PowerBASIC" NEXT M

Result

P234567890

Po34567890

Pow4567890

Powe567890

...

PowerBAS90

PowerBASI0

PowerBASIC