mid
Builtin index · Collections & strings
result = substr(s, start, [length])
Input: s, start, length · Output: result
What it is
s is a Str; start (number, 0-based character index, must lie in [0, char_count]) is where the slice begins; optional length (number of characters, default: to the end) is clamped rather than erroring if it runs past what's left. Plain aliases of the same implementation (mid exists purely for BASIC/Excel discoverability).
Overloads
One call shape — this function is not overloaded.
| Code | Input | Output | Description |
result = substr(s, start, [length]) | s Strs is a Str.start numberstart (number, 0-based character index, must lie in [0, char_count]) is where the slice begins.length number, optionaloptional length (number of characters, default: to the end) is clamped rather than erroring if it runs past what's left. | result Str | Returns a Str. |
Examples
From the documentation, and run by the build -- the output and the figure below are what the engine actually produced. Simplest first.
s = "Hello, World!"
print(substr(s, 7)) # "World!" — to the end
print(substr(s, 0, 5)) # "Hello" — bounded
print(substr(s, 7, 100)) # "World!" — clamped, not an error
print(mid(s, 7, 5)) # "World" — mid is substr under another name
World! Hello World! World
from collections-strings
s = "Hello, Qu World!"
print(mid(s, 7, 2)) # "Qu"
print(left(s, 5)) # "Hello"
print(right(s, 6)) # "World!"
print(reverse("abc")) # "cba"
print(lower("SHOUT")) # "shout"
print(ucase("shout")) # "SHOUT"
print(lcase("SHOUT")) # "shout"
print(toupper("mix")) # "MIX"
print(tolower("MIX")) # "mix"
print(capitalize("ada lovelace")) # "Ada lovelace"
print(ltrim(" padded")) # "padded"
print(rtrim("padded ") + "|") # "padded|"
print(pad_right("ab", 5, ".")) # "ab..."
print(compare("apple", "banana")) # -1
print(asc("A")) # 65
Qu
Hello
World!
cba
shout
SHOUT
shout
MIX
mix
Ada lovelace
padded
padded|
ab...
-1
... 1 more linesfrom collections-strings
How it works
All index/length arguments below are 0-based (matching every other index in this codebase) and unicode-aware: built on .chars(), never a raw byte slice, so a multi-byte UTF-8 string indexes by character, not by byte (a naive byte slice would panic or corrupt on non-ASCII input). substr(s, start[, length]) / mid(s, start, length) — plain aliases of the same implementation: MATLAB has no mid at all (that's a BASIC/Excel convention), and since Qu already indexes 0-based everywhere (unlike BASIC/Excel's 1-based MID$), there is no real semantic difference to preserve — mid exists purely for discoverability by BASIC/Excel habit. start must be within [0, char_count] (== char_count is the valid "nothing left" case, matching the empty-slice-at-the-end convention s[n:] uses elsewhere); length is clamped to whatever is actually available rather than erroring — asking for more than what's left (substr(s, 0, 1000) to mean "the rest") is a common, harmless overshoot, unlike a genuinely out-of-range start.
From the note above this builtin in the interpreter, where the reasoning was written down as it was built.
See also
substr — documented in the same entry.