String.join
func String.join(parts: Array[string], sep: string): stringString.join returns the strings in parts joined with sep between each two.
Details
sepgoes only between two parts, so an emptypartsgives""and an empty part keeps the separators on both sides of it.
Basic examples
Join words with a comma and a space:
String.join(["ant", "bee", "wasp"], ", ") // "ant, bee, wasp"Join lines with a line break:
println(String.join(["first", "second", "third"], "\n"))first
second
thirdScope
Join an empty array and an array of one part:
let none: Array[string] = []
String.join(none, ",") // ""
String.join(["only"], ",") // "only"An empty part still counts as a part, so the separators next to it stay:
String.join(["a", "", "b"], ",") // "a,,b"
String.join(["", ""], ",") // ","
String.join([""], ",") // ""The separator can be any string: empty, several code points long, or in any script:
String.join(["a", "b", "c"], "") // "abc"
String.join(["a", "b", "c"], " -> ") // "a -> b -> c"
String.join(["日本", "🐜"], "·") // "日本·🐜"Pass the parts with a pipe, which puts the array in as the first argument:
["x", "y", "z"] | String.join("/") // "x/y/z"Name the arguments, in either order:
String.join(sep: "+", parts: ["x", "y"]) // "x+y"The array can come from any expression, such as a map over numbers:
[1, 2, 3].map({ String.from(x) }) | String.join(" + ") // "1 + 2 + 3"Applications
Replace one separator with another:
"2026-11-08".split("-") | String.join("/") // "2026/11/08"Collapse every run of white space to one space:
" too many\tspaces \n".fields() | String.join(" ") // "too many spaces"Write a row of comma-separated values from a record:
type Ant = { name: string, legs: int, queen: bool }
func row(a: Ant): string {
String.join([a.name, String.from(a.legs), String.from(a.queen)], ",")
}
println(row(Ant{ name: "Formica", legs: 6, queen: false }))Formica,6,falseList names in a sentence, with "and" before the last one:
func listing(names: Array[string]): string {
let n = names.length()
if n < 2 {
return String.join(names, "")
}
String.join(names.slice(0, n - 1), ", ") + " and " + names[n - 1]
}
println(listing(["Ann"]))
println(listing(["Ann", "Bob"]))
println(listing(["Ann", "Bob", "Cy"]))Ann
Ann and Bob
Ann, Bob and CyProperties and relations
Joining with "" gives the same string as String.concat:
let parts = ["a", "bc", "", "d"]
assert(String.join(parts, "") == String.concat(parts))Splitting this string with String.split and joining it back with the same separator gives the string again:
let s = "a,b,,c"
assert(String.join(String.split(s, ","), ",") == s)For a non-empty parts, the length of the result is the sum of the lengths of the parts, plus one separator fewer than there are parts:
let parts = ["é", "日本", "", "🐜"]
let sep = ", "
let total = parts.map({ x.length() }).sum() ?? 0
assert(String.join(parts, sep).length() == total + (parts.length() - 1) * sep.length())Possible issues
parts.join(",") looks in the Array namespace, which has no join, so the call is a compile error. Call String.join(parts, ",") or use a pipe:
let s = ["a", "b"].join(",") // error: Array[string] has no field or function joinA string is not the subject either: String.join takes the array first:
let s = ",".join(["a", "b"])
// error: String.join takes Array[string] first, so a string does not reach itThe separator has no default. Leaving it out is a compile error; pass "" or use String.concat:
let s = String.join(["a", "b"]) // error: String.join needs an argument for sepEvery part must be a string. Convert other values with String.from first:
let s = String.join(["a", 1], ",") // error: cannot use int where string is expectedInterpolating an array does not join it: it renders the array with its brackets and quotes:
let xs = ["a", "b"]
"${xs}" // "[\"a\", \"b\"]"
"${String.join(xs, ", ")}" // "a, b"The separator goes only between parts, so text joined with "\n" has no final line break; add + "\n" when the text should end a line:
String.join(["a", "b"], "\n") // "a\nb"String.split does not always undo String.join. In these two cases the split gives back different parts:
String.join(["a,b", "c"], ",").split(",") // ["a", "b", "c"]
let none: Array[string] = []
String.join(none, ",").split(",") // [""]See also
String.concat, String.split, String.fields, String.lines, String.repeat, String.from, Path.join
