Difference between revisions of "MID$ (statement)"

From QB64 Wiki
Jump to navigation Jump to search
imported>Clippy
imported>Clippy
Line 23: Line 23:
:: position = [[INSTR]](start, a$, "dog")
:: position = [[INSTR]](start, a$, "dog")
:: IF position THEN  
:: IF position THEN  
::: MID$(a$, position, 3) = "rat" ' changes "dog" to "rat"
::: MID$(a$, position, 3) = "rat" ' changes "dog" to "rat" when found
::: start = position + 1 ' advance one string character
::: start = position + 1 ' advance one string character
:: END IF
:: END IF

Revision as of 04:20, 18 September 2009

The MID$ statement specifies a position in a STRING to overwrite previous data.


Syntax: MID$(string_variable, start%, length%) = string_value


  • String variable is any STRING or ASCII character values in quotes or a variable.
  • Start specifies the string character to start and length reserves the space.
  • The string value to be placed in the string should be as long as the length reserved.
  • The length of the original string is NOT changed in any case! Excess characters will not be in the string.


Example: Using INSTR to locate the string positions and a MID$ statement to change the word.


a$ = "The cats and dogs where playing, even though dogs don't like cats."
start = 1 ' start cannot be 0 when used in INSTR!


DO
position = INSTR(start, a$, "dog")
IF position THEN
MID$(a$, position, 3) = "rat" ' changes "dog" to "rat" when found
start = position + 1 ' advance one string character
END IF
LOOP UNTIL position = 0
PRINT a$


Screen Results: The cats and rats were playing, even though rats don't like cats.




See also: MKI$, MKL$, MKS$, MKD$, STR$, HEX$, INSTR, Bitmaps, ASCII


Navigation:

Go to Keyword Reference - Alphabetical

Go to Keyword Reference - By usage