Difference between revisions of "MID$ (statement)"

From QB64 Wiki
Jump to navigation Jump to search
imported>Clippy
m (Fix example highlighting/linking)
 
(16 intermediate revisions by 3 users not shown)
Line 1: Line 1:
The '''MID$''' statement specifies a position in a [[STRING]] to overwrite previous data.
The [[MID$]] statement substitutes one or more new characters for existing characters of a previously defined [[STRING]].






''Syntax:'' MID$(string_value$, start%, length%) = string_value
{{PageSyntax}}
: [[MID$]]({{Parameter|baseString$}}, {{Parameter|startPosition%}}[, {{Parameter|bytes%}}]) = {{Parameter|replacementString$}}




{{PageDescription}}
* 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|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.


* String value is any literal or variable [[STRING]] or [[ASCII]] character value.
* Start specifies the string character to start and length reserves the space. Cannot be 0!
* 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.


{{PageExamples}}
''Example:'' Using [[INSTR]] to locate the string positions and a [[MID$ (statement)|MID$]] statement to change the words.


''Example:'' Using [[INSTR]] to locate the string positions and a [[MID$ (statement)|MID$]] statement to change the word.
{{CodeStart}}
text$ = "The cats and dogs were playing, even though dogs don't like cats."
{{Cl|PRINT}} text$
start% = 1 ' start cannot be 0 when used in the INSTR function!
{{Cl|DO...LOOP|DO}}
    position% = {{Cl|INSTR}}(start%, text$, "dog")
    {{Cl|IF}} position% {{Cl|THEN}}                    ' when position is a value greater than 0
{{Cl|MID$}}(text$, position%, 3) = "rat" ' change "dog" to "rat" when found
start% = position% + 1            ' advance one position to search rest of string
    {{Cl|END IF}}
{{Cl|LOOP}} {{Cl|UNTIL}} position% = 0 ' no other matches found
{{Cl|PRINT}} text$ '' ''
{{CodeEnd}}
{{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}}




:a$ = "The cats and dogs where playing, even though dogs don't like cats."
{{PageSeeAlso}}
:start = 1 ' start cannot be 0 when used in the INSTR function!
* [[MID$]] {{text|(function)}}
* [[LEFT$]], [[RIGHT$]]
* [[INSTR]], [[ASCII]],  [[STR$]], [[HEX$]], [[Bitmaps]] (example)
* [[MKI$]], [[MKL$]], [[MKS$]], [[MKD$]]




:DO
{{PageNavigation}}
:: 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 so no repeats
:: 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:==
 
[[Keyword_Reference_-_Alphabetical|Go to Keyword Reference - Alphabetical]]
 
[[Keyword Reference - By usage|Go to Keyword Reference - By usage]]

Latest revision as of 09:26, 23 April 2021

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


Syntax

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


Description

  • 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" ' 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



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