MID$ statement   

Purpose

Replace characters in a string with characters from another string.

Syntax

MID$(string_var, start& [, length&]) = replacement

Remarks

start& and length& are numeric variables or expressions.  As a statement, MID$ replaces length& characters of string_var, beginning at character position start&, with the contents of replacement string.

If length& is included, it determines how many characters of replacement string are inserted into string_var.  If length& is omitted, all of replacement string is used.  If length& is negative, it is interpreted as LEN(string_var)-ABS(length&).  For example, MID$("ABCDEFGHIJK",3,-7) = "*******" yields "AB****GHIJK".

If start& is negative, the starting position is assumed to be start& characters from the end of the string.  MID$("abcde", -3, 2) = "123" yields "ab12e", while the statement MID$("abcde", -3) = "123" yields "ab123".

The replacement will never extend past the end of the original string_var; that is, MID$ never alters the length of a string.  For a similar function that can alter the length of a string, please refer to the REPLACE statement.

Restrictions

If start& evaluates to a position outside of the string on either side, or if start& is zero, no operation is performed.

The MID$ statement cannot be used to extend the length of a string.  If start is beyond the end of a string, the statement is ignored.

See also

BUILD$, INSTR, LTRIM$, MID$ functionREMOVE$, 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