Difference between revisions of "MID$ (statement)"

From QB64 Wiki
Jump to navigation Jump to search
imported>Clippy
m
imported>Clippy
m
Line 1: Line 1:
The '''MID$''' statement overwrites a portion of a previously defined [[STRING]].
The '''MID$''' statement substitutes one or more new characters for existing characters of a previously defined [[STRING]].






{{PageSyntax}}
{{PageSyntax}}
:: MID$(basestring$, start_position%[, bytes%]) = string_to_add$
:: MID$(''basestring$'', ''start_position%''[, ''bytes%'']) = string_to_add$






* String value is any literal or variable [[STRING]] and/or [[ASCII]] character values.
* The ''basestring'' variable [[STRING]] value must exist and be large enough to contain the ''string_to_add''.
* Start position specifies the string character position to start the overwrite. Cannot be 0 or an [[ERROR Codes|Illegal function call error]] will occur!
* ''Start position'' specifies the string character position to start the overwrite. Cannot be 0 or an [[ERROR Codes|Illegal function call error]] will occur!
* The byte length is optional. Excess statement string characters or byte lenghts are ignored.
* The ''byte length'' or number of characters is optional. Excess statement string characters or byte lenghts are ignored.
* The string value to be placed in the string should be as long as the byte length reserved.
* The string value to be placed in the string should be as long as the byte length reserved.
* The length of the original string is NOT changed in any case! Excess characters will not be in the string value returned.
* The length of the original string is NOT changed in any case! Excess characters will not be in the string value returned.

Revision as of 07:06, 2 October 2011

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


Syntax

MID$(basestring$, start_position%[, bytes%]) = string_to_add$


  • The basestring variable STRING value must exist and be large enough to contain the string_to_add.
  • Start position specifies the string character position to start the overwrite. Cannot be 0 or an Illegal function call error will occur!
  • The byte length or number of characters is optional. Excess statement string characters or byte lenghts are ignored.
  • The string value to be placed in the string should be as long as the byte length reserved.
  • The length of the original string is NOT changed in any case! Excess characters will not be in the string value returned.


Example: Using INSTR to locate the string positions and a MID$ 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" ' changes "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:



Navigation:
Keyword Reference - Alphabetical
Keyword Reference - By Usage
Main Wiki Page