types.String.split
Splits str into an array of strings.
... from the prose below, not from the signature.Parameters
separator | string | the literal substring to split on; if omitted, str is split on runs of white space instead optional |
... | any | not 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
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())" a b\tc ".split() joined = a,b,c
"a::b::c".split("::") joined = a,b,c
"a,b,".split(",").len() = 3See also
split_by_chars | Splits str into an array of strings at every byte that occurs in separators. |
join | Joins the items of arr into one string, with str between each pair. |
| string | class index |