Quirrel4.41.0

types.String.split_by_chars

Method on every string
string.split_by_chars(separators: string, [skip_empty: bool], ...): any

Splits str into an array of strings at every byte that occurs in separators.

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.
examples/types/string/split_by_chars-basic.nut
let inventory = "sword,shield,potion"
println("inventory.split_by_chars(\",\") joined =", ",".join(inventory.split_by_chars(",")))
Output:
inventory.split_by_chars(",") joined = sword,shield,potion

Parameters

separatorsstringthe set of bytes that split str, each one its own separator, not a substring to match as a whole
skip_emptyboolwhen true, a piece with zero bytes is left out of the result instead of being added as "" optional
...any repeats

Return value

An array of the pieces of str, in order, with every separator byte removed. See string.split_by_chars for the exact rules on leading, trailing and repeated separators.

Errors

Throws empty separators string when separators is "".

Notes

Takes 1 or 2 arguments: skip_empty is optional and defaults to false. Same implementation as string.split_by_chars; see the Notes on types.String.strip for how the two forms differ only in their reported attributes, not their behavior.

Contrast types.String.split, whose separator is a literal substring matched as a whole rather than a set of individual bytes.

Example

examples/types/string/split_by_chars.nut
println("\"a,,b,\".split_by_chars(\",\") joined =", ",".join("a,,b,".split_by_chars(",")))     // trailing separator: no trailing empty
println("\"a,,b,\".split_by_chars(\",\", true) joined =", ",".join("a,,b,".split_by_chars(",", true))) // skip_empty drops the middle piece too
Output:
"a,,b,".split_by_chars(",") joined = a,,b
"a,,b,".split_by_chars(",", true) joined = a,b

See also

splitSplits str into an array of strings.
split_by_charsSplits str into an array of strings at every character that occurs in separators.
stringclass index