Quirrel4.41.0

types.String.split

Method on every string
pure string.split([separator: string], ...): any

Splits str into an array of strings.

The binding carries no declaration string, so the VM cannot report parameter names. The names above are from this page; the types and attributes still come from the VM. The VM also cannot tell an optional parameter from a variadic tail here, so read the brackets and any trailing ... from the prose below, not from the signature.

Parameters

separatorstringthe literal substring to split on; if omitted, str is split on runs of white space instead optional
...anynot a real parameter; see Notes repeats

Return value

With no separator, str is split on runs of white space (as in string.strip): leading and trailing white space produce no empty pieces, and a run of several white-space bytes counts as one separator.

With separator given, str is split wherever that exact substring occurs, matched as a whole rather than as a set of bytes - contrast types.String.split_by_chars, which treats each byte of its argument as its own separator. Every match produces a piece, including empty ones: a leading or repeated separator makes an empty piece, and, unlike split_by_chars, a trailing separator also makes an empty piece at the end, because this form always emits whatever follows the last match, even nothing.

Errors

Throws empty separator when separator is "".

Notes

Takes 0 or 1 argument, not the variadic tail the VM's dump implies (it shows split([arg1: string], ...) because this binding carries no declaration string). A second argument is silently ignored.

Example

examples/types/string/split.nut
println("\" a  b\\tc \".split() joined =", ",".join(" a  b\tc ".split()))     // no separator: split on white space runs

// a literal substring separator, unlike split_by_chars's set of bytes.
println("\"a::b::c\".split(\"::\") joined =", ",".join("a::b::c".split("::")))

// a trailing separator DOES make a trailing empty piece here.
println("\"a,b,\".split(\",\").len() =", "a,b,".split(",").len())
Output:
" a  b\tc ".split() joined = a,b,c
"a::b::c".split("::") joined = a,b,c
"a,b,".split(",").len() = 3

See also

split_by_charsSplits str into an array of strings at every byte that occurs in separators.
joinJoins the items of arr into one string, with str between each pair.
stringclass index