QB64.com

QB64 is a modern extended BASIC programming language that retains QBasic/QuickBASIC 4.5 compatibility and compiles native binaries for Windows, Linux, and macOS.

The MID$ statement substitutes one or more new characters for existing characters of a previously defined STRING.

Syntax

MID$(baseString$, startPosition%[, bytes%]) = replacementString$

Description

Example(s)

Using INSTR to locate the string positions and a MID$ (statement) statement to change the words.


 text$ = "The cats and dogs were playing, even though dogs don't like cats."
 PRINT text$ 
 start% = 1          ' start cannot be 0 when used in the INSTR function!
 DO
   position% = INSTR(start%, text$, "dog")
   IF position% THEN            ' when position is a value greater than 0
	MID$(text$, position%, 3) = "rat" ' change "dog" to "rat" when found
     start% = position% + 1     ' advance one position to search rest of string
   END IF
 LOOP UNTIL position% = 0       ' no other matches found
 PRINT text$ 


The cats and dogs were playing, even though dogs don't like cats.
The cats and rats were playing, even though rats don't like cats.

See Also