MID\$()

## Effect

MID\$ is kind of special as it can be used both as function or as instruction:
- As function it returns a substring of variable length (y) starting at a variable position (x) in an input string A\$
- As instruction it can be used to replace substring by another string.

Remark: For strings with Japanese or Korean characters, you need to use CALL KMID.

## Syntaxes

MID\$(A\$,x[,y])

### Instruction Syntax

MID\$(A\$,x[,y])=<string>

## Range

MID\$ accepts an offset (x) between 1 and 255 and an optional length (y) between 0 and 255.

If no length (y) is specified then
In case of function the entire substring starting at position x is returned
In case of instruction the entire replacement string is used at position x

When (x) = 1, MID\$ function replaces the LEFT\$ function
MID\$(A\$,1,3) is equivalent to LEFT\$(A\$,3)

When (x) = LEN(A\$)-(y)+1, MID\$ function replaces the RIGHT\$ function
If LEN(A\$)=5 then MID\$(A\$,4,2) is equivalent to RIGHT\$(A\$,2)

## Examples

MID\$ as function

```10 A\$="www.msx.org"
20 PRINT MID\$(A\$,5,3)
30 PRINT MID\$(A\$,5)

RUN```
```msx
msx.org```

MID\$ as instruction

```10 A\$="ABCDEFG"
20 PRINT A\$
30 MID\$(A\$,4)="XYZ"
40 PRINT A\$

RUN```
```ABCDEFG
ABCXYZG
```

## Compatibility

MSX-BASIC 1.0 or higher