MID$(string$, start [, count])pb_cg_emit_mid_builtin (pb_codegen.c:2932)Returns a substring of string$ starting at position start (1-based). If count is provided, that many characters are returned. If count is omitted, all characters from start to the end of the string are returned.
The start parameter is clamped: values less than 1 are treated as 1. If start exceeds the string length, an empty string is returned. MID$ is the most flexible substring function, capable of extracting any contiguous region from a string.
Common uses include parsing delimited data (e.g., CSV fields), extracting substrings by position, reading fixed-width record formats, and isolating portions of path or identifier strings.
| Parameter | Description |
|---|---|
string$ | The source STRING expression. Can be a literal, variable, or expression. |
start | 1-based starting position within string$. Values less than 1 are clamped to 1. A numeric expression. |
count | (Optional) Number of characters to extract. If omitted, all characters from start to the end are returned. A numeric expression. |
A new STRING containing the extracted substring, or an empty string ("") if start is beyond the string length.
FUNCTION PBMAIN() AS LONG
LOCAL s$, part$ AS STRING
s$ = "Hello World"
' With count: extract "World"
part$ = MID$(s$, 7, 5) ' part$ = "World"
PRINT "Chars 7-11: "; part$
' Without count: extract rest
part$ = MID$(s$, 7) ' part$ = "World"
PRINT "From pos 7: "; part$
' Start at 1: equivalent to full string
part$ = MID$(s$, 1) ' part$ = "Hello World"
PRINT "Full string: "; part$
' Start beyond length: empty result
part$ = MID$(s$, 100, 5) ' part$ = ""
PRINT "Empty: ["; part$; "]"
' Clamped start (less than 1 becomes 1)
part$ = MID$(s$, 0, 5) ' part$ = "Hello"
PRINT "Clamped: "; part$
' Practical: parse fixed-width data
s$ = "John Doe 35 NY "
part$ = MID$(s$, 1, 8) ' part$ = "John "
PRINT "First: ["; part$; "]"
part$ = MID$(s$, 9, 8) ' part$ = "Doe "
PRINT "Last: ["; part$; "]"
part$ = MID$(s$, 17, 4) ' part$ = "35 "
PRINT "Age: ["; part$; "]"
FUNCTION = 0
END FUNCTION
pb_cg_emit_mid_builtin (pb_codegen.c:2932); Clamp start >= 1, convert to 0-based cmp rax, 1 jge __pb_mid_start_ok_N mov rax, 1 __pb_mid_start_ok_N: sub rax, 1 ; 0-based index ; If start >= source_len -> empty result cmp rax, [rbp - written_offset] jae __pb_mid_empty_N ; Compute count (or remaining length), clamp to buffer ; Copy from source[start] for count+1 bytes mov rdx, [rbp - print_ptr_offset] add rdx, rax ; source + start lea rcx, [rbp - asciiz_scratch] mov r8, [rbp - print_len_offset] add r8, 1 call [__imp_lstrcpynA] jmp __pb_mid_done_N __pb_mid_empty_N: mov byte [rbp - asciiz_scratch], 0 ; empty string __pb_mid_done_N:
MID$ is a function (returns a value). The older MID$() = value statement form (mid-statement for in-place replacement) is not supported by this backend.count is omitted, the remaining length (source_len - start) is used.lstrcpynA handles the copy and guarantees NUL-termination even if the source fragment is not NUL-terminated.