MID$: Difference between revisions

From QB64 Phoenix Edition Wiki
Jump to navigation Jump to search
No edit summary
No edit summary
 
(3 intermediate revisions by the same user not shown)
Line 1: Line 1:
The [[MID$]] function returns a portion of a [[STRING|string]].
The '''MID$''' statement substitutes one or more new characters for existing characters of a previously defined [[STRING]].




{{PageSyntax}}
{{PageSyntax}}
: {{Parameter|portion$}} = [[MID$]]({{Parameter|stringValue$}}, {{Parameter|startPosition%}}[, {{Parameter|bytes%}}])
: [[MID$]]({{Parameter|baseString$}}, {{Parameter|startPosition%}}[, {{Parameter|bytes%}}]) = {{Parameter|replacementString$}}




{{PageParameters}}
{{PageParameters}}
* {{Parameter|stringValue$}} can be any literal or variable non-empty [[STRING]] value. Use [[LEN]] to check the length of a string.
* The {{Parameter|baseString$}} variable must exist and be large enough to contain the {{Parameter|replacementString$}}.
* {{Parameter|startPosition%}} designates the non-zero position of the first character to be returned by the function.
* {{Parameter|startPosition%}} specifies the string character position to start the overwrite.
* {{Parameter|bytes%}} (optional) tells the function how many characters to return including the first character at {{Parameter|startPosition%}}.
* {{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.
{{PageDescription}}
* When the {{Parameter|bytes%}} value is not passed, the function returns the remainder of the string from the starting character position.
* Number of character {{Parameter|bytes%}} should be within the string's [[LEN|length]] from the start position, but will only return the string's remainder when exceeded.
* If the {{Parameter|bytes%}} value is 0 or the {{Parameter|startPosition%}} is 0 or greater than the [[LEN|length]] of the string, an empty string is returned (no error is triggered).
* In '''QB64''', [[ASC]] string byte position reads are about '''5 times faster''' than MID$ when parsing strings. See ''Example 2'' below.
 
=== QBasic/QuickBASIC ===
* In QBasic the {{Parameter|startPosition%}} could not be zero (0) or an [[ERROR Codes|Illegal function call error]] would occur.




{{PageExamples}}
{{PageExamples}}
''Example 1:'' Getting the hour and minutes from [[TIME$]]
;Example:Using [[INSTR]] to locate the string positions and a '''MID$''' statement to change the words.
{{CodeStart}}
{{CodeStart}}
{{Cl|PRINT}} {{Cl|TIME$}}
text$ = "The cats and dogs were playing, even though dogs don't like cats."
 
PRINT text$
hour$ = {{Cl|LEFT$}}({{Cl|TIME$}}, 2)
start% = 1          ' start cannot be 0 when used in the INSTR function!
minutes$ = {{Cl|MID$}}({{Cl|TIME$}}, 4, 2) ' skip hours and the colon (first 3 characters)
{{Cl|DO...LOOP|DO}}
 
  position% = {{Cl|INSTR}}(start%, text$, "dog")
{{Cl|PRINT}} "hour = "; hour$; ": minutes = "; minutes$
  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}}11:23:30
{{OutputStart}}
hour = 11: minutes = 23
The cats and dogs were playing, even though dogs don't like cats.
{{OutputEnd}}
The cats and rats were playing, even though rats don't like cats.
 
 
''Example 2:'' Comparing MID$, the '''QB64''' byte position version of [[ASC]] and [[_MEMGET]] speeds parsing string characters:
{{CodeStart}}
{{Cl|_TITLE}} "String Speed Test"
{{Cl|DEFLNG}} A-Z
 
'First let's build a string for testing.
Limit = 100000 'the size of the string
LoopCount = 1000 'the number of times we want to deconstruct it
 
{{Cl|FOR...NEXT|FOR}} i = 1 {{Cl|TO}} Limit
  t$ = t$ + {{Cl|CHR$}}({{Cl|RND}} * 255)
{{Cl|NEXT}}
 
'now for some times
 
t1# = {{Cl|TIMER}}
{{Cl|FOR...NEXT|FOR}} j = 1 {{Cl|TO}} LoopCount
  {{Cl|FOR...NEXT|FOR}} i = 1 {{Cl|TO}} Limit
    m$ = {{Cl|MID$}}(t$, i, 1)
  {{Cl|NEXT}}
{{Cl|NEXT}}
t2# = {{Cl|TIMER}}
{{Cl|FOR...NEXT|FOR}} j = 1 {{Cl|TO}} LoopCount
  {{Cl|FOR...NEXT|FOR}} i = 1 {{Cl|TO}} Limit
    m = {{Cl|ASC}}(t$, i)
  {{Cl|NEXT}}
{{Cl|NEXT}}
 
t3# = {{Cl|TIMER}}
{{Cl|$CHECKING}}:OFF
{{Cl|DIM}} m {{Cl|AS}} {{Cl|_MEM}}, m1 {{Cl|AS}} {{Cl|STRING}} * 1, m2 {{Cl|AS}} {{Cl|_UNSIGNED}} {{Cl|_BYTE}}
m = {{Cl|_MEMNEW}}(Limit) 'create new memory space for string
{{Cl|_MEMPUT}} m, m.OFFSET, t$ 'put string t$ into memory space
{{Cl|FOR...NEXT|FOR}} j = 1 {{Cl|TO}} LoopCount
  {{Cl|FOR...NEXT|FOR}} i = 1 {{Cl|TO}} Limit
    {{Cl|_MEMGET}} m, m.OFFSET + i - 1, m1
  {{Cl|NEXT}}
{{Cl|NEXT}}
t4# = {{Cl|TIMER}}
{{Cl|FOR...NEXT|FOR}} j = 1 {{Cl|TO}} LoopCount
  {{Cl|FOR...NEXT|FOR}} i = 1 {{Cl|TO}} Limit
    {{Cl|_MEMGET}} m, m.OFFSET + i - 1, m2
  {{Cl|NEXT}}
{{Cl|NEXT}}
t5# = {{Cl|TIMER}}
 
'results
 
{{Cl|PRINT USING}} "##.###### seconds for MID$"; t2# - t1#
{{Cl|PRINT USING}} "##.###### seconds for ASC"; t3# - t2#
{{Cl|PRINT USING}} "##.###### seconds for _MEMGET String"; t4# - t3#
{{Cl|PRINT USING}} "##.###### seconds for _MEMGET Byte"; t5# - t4#
{{CodeEnd}} {{small|Code by Steve McNeill}}
{{OutputStart}}6.593750 seconds for MID$
1.044922 seconds for ASC
0.494141 seconds for _MEMGET String
0.494141 seconds for _MEMGET Byte
{{OutputEnd}}
{{OutputEnd}}
: ''Note:'' [[_MEMGET]] can be used with [[$CHECKING]]:OFF to cut the parsing speed even more. [[STRING]] * 1 or [[_BYTE]] are similar speeds.




''See also:''
{{PageSeeAlso}}
* [[MID$ (statement)]], [[ASC]]
* [[MID$ (function)]]
* [[ASC]], [[ASC (function)]]
* [[LEFT$]], [[RIGHT$]]
* [[LEFT$]], [[RIGHT$]]
* [[LTRIM$]], [[RTRIM$]]
* [[INSTR]], [[ASCII]],  [[STR$]], [[HEX$]], [[Bitmaps]]
* [[INSTR]], [[LEN]]
* [[MKI$]], [[MKL$]], [[MKS$]], [[MKD$]]
* [[_MEMPUT]], [[_MEMGET]]




{{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



Navigation:
Main Page with Articles and Tutorials
Keyword Reference - Alphabetical
Keyword Reference - By usage
Report a broken link