MID$ (function): Difference between revisions
Jump to navigation
Jump to search
Code by Steve McNeill
Navigation:
Main Page with Articles and Tutorials
Keyword Reference - Alphabetical
Keyword Reference - By usage
Report a broken link
(Created page with "The MID$ function returns a portion of a string. {{PageSyntax}} : {{Parameter|portion$}} = MID$({{Parameter|stringValue$}}, {{Parameter|startPosition%}}[, {{Parameter|bytes%}}]) {{PageParameters}} * {{Parameter|stringValue$}} can be any literal or variable non-empty STRING value. Use LEN to check the length of a string. * {{Parameter|startPosition%}} designates the non-zero position of the first character to be returned by the function. * {...") |
No edit summary |
||
Line 1: | Line 1: | ||
The | The '''MID$''' function returns a portion of a [[STRING]]. | ||
{{PageSyntax}} | {{PageSyntax}} | ||
: {{Parameter|portion$}} = [[MID$]]({{Parameter|stringValue$}}, {{Parameter|startPosition%}}[, {{Parameter|bytes%}}]) | : {{Parameter|portion$}} = [[MID$ (function)|MID$]]({{Parameter|stringValue$}}, {{Parameter|startPosition%}}[, {{Parameter|bytes%}}]) | ||
Line 14: | Line 14: | ||
{{PageDescription}} | {{PageDescription}} | ||
* When the {{Parameter|bytes%}} value is not passed, the function returns the remainder of the string from the starting character position. | * 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 | * Number of character {{Parameter|bytes%}} should be within the string's 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 | * If the {{Parameter|bytes%}} value is 0 or the {{Parameter|startPosition%}} is 0 or greater than the length of the string, an empty string is returned (no error is triggered). | ||
* In '''QB64''', [[ASC]] string byte | * In '''QB64''', the [[ASC (function)]] reads string byte positions about 5 times faster than '''MID$''' when parsing strings character wise. See ''Example 2'' below. | ||
=== QBasic/QuickBASIC === | === QBasic/QuickBASIC === | ||
Line 23: | Line 23: | ||
{{PageExamples}} | {{PageExamples}} | ||
;Example 1:Getting the hour and minutes from [[TIME$]]. | |||
{{CodeStart}} | {{CodeStart}} | ||
{{Cl|PRINT}} {{Cl|TIME$}} | {{Cl|PRINT}} {{Cl|TIME$}} | ||
hour$ = {{Cl|LEFT$}}({{Cl|TIME$}}, 2) | hour$ = {{Cl|LEFT$}}({{Cl|TIME$}}, 2) | ||
minutes$ = {{Cl|MID$}}({{Cl|TIME$}}, 4, 2) ' skip hours and the colon (first 3 characters) | minutes$ = {{Cl|MID$ (function)|MID$}}({{Cl|TIME$}}, 4, 2) ' skip hours and the colon (first 3 characters) | ||
{{Cl|PRINT}} "hour = "; hour$; ": minutes = "; minutes$ | {{Cl|PRINT}} "hour = "; hour$; ": minutes = "; minutes$ | ||
{{CodeEnd}} | {{CodeEnd}} | ||
{{OutputStart}}11:23:30 | {{OutputStart}} | ||
11:23:30 | |||
hour = 11: minutes = 23 | hour = 11: minutes = 23 | ||
{{OutputEnd}} | {{OutputEnd}} | ||
---- | |||
Example 2:Comparing '''MID$''', the '''QB64''' byte position version of the [[ASC (function)]] and [[_MEMGET]] speeds parsing string characters. | |||
{{CodeStart}} | {{CodeStart}} | ||
{{Cl|_TITLE}} "String Speed Test" | {{Cl|_TITLE}} "String Speed Test" | ||
Line 55: | Line 57: | ||
{{Cl|FOR...NEXT|FOR}} j = 1 {{Cl|TO}} LoopCount | {{Cl|FOR...NEXT|FOR}} j = 1 {{Cl|TO}} LoopCount | ||
{{Cl|FOR...NEXT|FOR}} i = 1 {{Cl|TO}} Limit | {{Cl|FOR...NEXT|FOR}} i = 1 {{Cl|TO}} Limit | ||
m$ = {{Cl|MID$}}(t$, i, 1) | m$ = {{Cl|MID$ (function)|MID$}}(t$, i, 1) | ||
{{Cl|NEXT}} | {{Cl|NEXT}} | ||
{{Cl|NEXT}} | {{Cl|NEXT}} | ||
Line 61: | Line 63: | ||
{{Cl|FOR...NEXT|FOR}} j = 1 {{Cl|TO}} LoopCount | {{Cl|FOR...NEXT|FOR}} j = 1 {{Cl|TO}} LoopCount | ||
{{Cl|FOR...NEXT|FOR}} i = 1 {{Cl|TO}} Limit | {{Cl|FOR...NEXT|FOR}} i = 1 {{Cl|TO}} Limit | ||
m = {{Cl|ASC}}(t$, i) | m = {{Cl|ASC (function)|ASC}}(t$, i) | ||
{{Cl|NEXT}} | {{Cl|NEXT}} | ||
{{Cl|NEXT}} | {{Cl|NEXT}} | ||
Line 96: | Line 98: | ||
0.494141 seconds for _MEMGET Byte | 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. | |||
{{PageSeeAlso}} | {{PageSeeAlso}} | ||
* [[MID$ | * [[MID$]] | ||
* [[ASC]], [[ASC (function)]] | |||
* [[LEFT$]], [[RIGHT$]] | * [[LEFT$]], [[RIGHT$]] | ||
* [[LTRIM$]], [[RTRIM$]] | * [[LTRIM$]], [[RTRIM$]] |
Revision as of 00:40, 26 February 2023
The MID$ function returns a portion of a STRING.
Syntax
- portion$ = MID$(stringValue$, startPosition%[, bytes%])
Parameters
- stringValue$ can be any literal or variable non-empty STRING value. Use LEN to check the length of a string.
- startPosition% designates the non-zero position of the first character to be returned by the function.
- bytes% (optional) tells the function how many characters to return including the first character at startPosition%.
Description
- When the bytes% value is not passed, the function returns the remainder of the string from the starting character position.
- Number of character bytes% should be within the string's length from the start position, but will only return the string's remainder when exceeded.
- If the bytes% value is 0 or the startPosition% is 0 or greater than the length of the string, an empty string is returned (no error is triggered).
- In QB64, the ASC (function) reads string byte positions about 5 times faster than MID$ when parsing strings character wise. See Example 2 below.
QBasic/QuickBASIC
- In QBasic the startPosition% could not be zero (0) or an Illegal function call error would occur.
Examples
- Example 1
- Getting the hour and minutes from TIME$.
PRINT TIME$ hour$ = LEFT$(TIME$, 2) minutes$ = MID$(TIME$, 4, 2) ' skip hours and the colon (first 3 characters) PRINT "hour = "; hour$; ": minutes = "; minutes$ |
11:23:30 hour = 11: minutes = 23 |
Example 2:Comparing MID$, the QB64 byte position version of the ASC (function) and _MEMGET speeds parsing string characters.
_TITLE "String Speed Test" 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 FOR i = 1 TO Limit t$ = t$ + CHR$(RND * 255) NEXT 'now for some times t1# = TIMER FOR j = 1 TO LoopCount FOR i = 1 TO Limit m$ = MID$(t$, i, 1) NEXT NEXT t2# = TIMER FOR j = 1 TO LoopCount FOR i = 1 TO Limit m = ASC(t$, i) NEXT NEXT t3# = TIMER $CHECKING:OFF DIM m AS _MEM, m1 AS STRING * 1, m2 AS _UNSIGNED _BYTE m = _MEMNEW(Limit) 'create new memory space for string _MEMPUT m, m.OFFSET, t$ 'put string t$ into memory space FOR j = 1 TO LoopCount FOR i = 1 TO Limit _MEMGET m, m.OFFSET + i - 1, m1 NEXT NEXT t4# = TIMER FOR j = 1 TO LoopCount FOR i = 1 TO Limit _MEMGET m, m.OFFSET + i - 1, m2 NEXT NEXT t5# = TIMER 'results PRINT USING "##.###### seconds for MID$"; t2# - t1# PRINT USING "##.###### seconds for ASC"; t3# - t2# PRINT USING "##.###### seconds for _MEMGET String"; t4# - t3# PRINT USING "##.###### seconds for _MEMGET Byte"; t5# - t4# |
6.593750 seconds for MID$ 1.044922 seconds for ASC 0.494141 seconds for _MEMGET String 0.494141 seconds for _MEMGET Byte |
- Note
- _MEMGET can be used with $CHECKING:OFF to cut the parsing speed even more. STRING * 1 or _BYTE are similar speeds.
See also