MID$: Difference between revisions
Jump to navigation
Jump to search
Navigation:
Main Page with Articles and Tutorials
Keyword Reference - Alphabetical
Keyword Reference - By usage
Report a broken link
No edit summary |
No edit summary |
||
(3 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
The | The '''MID$''' statement substitutes one or more new characters for existing characters of a previously defined [[STRING]]. | ||
{{PageSyntax}} | {{PageSyntax}} | ||
: | : [[MID$]]({{Parameter|baseString$}}, {{Parameter|startPosition%}}[, {{Parameter|bytes%}}]) = {{Parameter|replacementString$}} | ||
{{PageParameters}} | {{PageParameters}} | ||
* {{Parameter| | * The {{Parameter|baseString$}} variable must exist and be large enough to contain the {{Parameter|replacementString$}}. | ||
* {{Parameter|startPosition%}} specifies the string character position to start the overwrite. | |||
* {{Parameter| | * {{Parameter|bytes%}} or number of characters is optional. Excess byte lenghts are ignored. | ||
* The {{Parameter|replacementString$}} should be as long as the byte length reserved. | |||
* The length of the original string is not changed in any case. If {{Parameter|replacementString$}} is longer, it gets clipped. | |||
* | |||
* | |||
* | |||
{{PageExamples}} | {{PageExamples}} | ||
;Example:Using [[INSTR]] to locate the string positions and a '''MID$''' statement to change the words. | |||
{{CodeStart}} | {{CodeStart}} | ||
{{Cl| | 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! | |||
{{Cl|DO...LOOP|DO}} | |||
position% = {{Cl|INSTR}}(start%, text$, "dog") | |||
IF position% THEN ' when position is a value greater than 0 | |||
{{Cl|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$ | |||
{{CodeEnd}} | {{CodeEnd}} | ||
{{OutputStart}} | {{OutputStart}} | ||
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. | |||
{{OutputEnd}} | {{OutputEnd}} | ||
{{PageSeeAlso}} | |||
* [[MID$ ( | * [[MID$ (function)]] | ||
* [[ASC]], [[ASC (function)]] | |||
* [[LEFT$]], [[RIGHT$]] | * [[LEFT$]], [[RIGHT$]] | ||
* [[ | * [[INSTR]], [[ASCII]], [[STR$]], [[HEX$]], [[Bitmaps]] | ||
* [[ | * [[MKI$]], [[MKL$]], [[MKS$]], [[MKD$]] | ||
{{PageNavigation}} | {{PageNavigation}} |
Latest revision as of 00:40, 26 February 2023
The MID$ statement substitutes one or more new characters for existing characters of a previously defined STRING.
Syntax
- MID$(baseString$, startPosition%[, bytes%]) = replacementString$
Parameters
- The baseString$ variable must exist and be large enough to contain the replacementString$.
- startPosition% specifies the string character position to start the overwrite.
- bytes% or number of characters is optional. Excess byte lenghts are ignored.
- The replacementString$ should be as long as the byte length reserved.
- The length of the original string is not changed in any case. If replacementString$ is longer, it gets clipped.
Examples
- 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
- MID$ (function)
- ASC, ASC (function)
- LEFT$, RIGHT$
- INSTR, ASCII, STR$, HEX$, Bitmaps
- MKI$, MKL$, MKS$, MKD$