Quirrel4.41.0

types.String.join

Method on every string
pure string.join(arr: array, [filter: bool|function], ...): any

Joins the items of arr into one string, with str between each pair.

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

arrarrayarray of items to join; each is converted with tostring()
filterbool|functionselects which items are kept; see Notes optional
...anynot a real parameter; a third explicit argument throws instead (see Errors) repeats

Return value

The items of arr, converted with tostring() and joined with str between each pair. An empty arr (or one where every item is filtered out) gives "".

Errors

Throws Too many arguments when called with more than 2 explicit arguments - unlike most methods on this page, join enforces its own upper bound instead of silently ignoring the extra. Throws a type-check error naming array if arr is not an array.

Notes

Takes 1 or 2 arguments: filter is optional. If filter is a bool, true drops every null item and every empty-string item from the result; false (or omitting filter) keeps everything. If filter is a function, it is called once per item and only items for which it returns a truthy value are kept, e.g. function(item) { return item.len() > 1 }.

Example

examples/types/string/join.nut
println("\",\".join([\"a\", \"b\", \"c\"]) =", ",".join(["a", "b", "c"]))

// bool filter: drop null and empty-string items.
println("\",\".join([\"a\", \"\", null, \"b\"], true) =", ",".join(["a", "", null, "b"], true))

// function filter: keep items the predicate accepts.
println("\",\".join([\"a\", \"bb\", \"ccc\"], filter) =", ",".join(["a", "bb", "ccc"], function(item) { return item.len() > 1 }))
Output:
",".join(["a", "b", "c"]) = a,b,c
",".join(["a", "", null, "b"], true) = a,b
",".join(["a", "bb", "ccc"], filter) = bb,ccc

See also

concatJoins its arguments into one string, with str between each pair.
splitSplits str into an array of strings.
stringclass index