Skip to content

String.join ​

ant
func String.join(parts: Array[string], sep: string): string

String.join returns the strings in parts joined with sep between each two.

Details ​

  • sep goes only between two parts, so an empty parts gives "" and an empty part keeps the separators on both sides of it.

Basic examples ​

Join words with a comma and a space:

ant
String.join(["ant", "bee", "wasp"], ", ") // "ant, bee, wasp"

Join lines with a line break:

ant
println(String.join(["first", "second", "third"], "\n"))
text
first
second
third

Scope ​

Join an empty array and an array of one part:

ant
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:

ant
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:

ant
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:

ant
["x", "y", "z"] | String.join("/") // "x/y/z"

Name the arguments, in either order:

ant
String.join(sep: "+", parts: ["x", "y"]) // "x+y"

The array can come from any expression, such as a map over numbers:

ant
[1, 2, 3].map({ String.from(x) }) | String.join(" + ") // "1 + 2 + 3"

Applications ​

Replace one separator with another:

ant
"2026-11-08".split("-") | String.join("/") // "2026/11/08"

Collapse every run of white space to one space:

ant
"  too   many\tspaces \n".fields() | String.join(" ") // "too many spaces"

Write a row of comma-separated values from a record:

ant
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 }))
text
Formica,6,false

List names in a sentence, with "and" before the last one:

ant
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"]))
text
Ann
Ann and Bob
Ann, Bob and Cy

Properties and relations ​

Joining with "" gives the same string as String.concat:

ant
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:

ant
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:

ant
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:

ant
let s = ["a", "b"].join(",") // error: Array[string] has no field or function join

A string is not the subject either: String.join takes the array first:

ant
let s = ",".join(["a", "b"])
// error: String.join takes Array[string] first, so a string does not reach it

The separator has no default. Leaving it out is a compile error; pass "" or use String.concat:

ant
let s = String.join(["a", "b"]) // error: String.join needs an argument for sep

Every part must be a string. Convert other values with String.from first:

ant
let s = String.join(["a", 1], ",") // error: cannot use int where string is expected

Interpolating an array does not join it: it renders the array with its brackets and quotes:

ant
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:

ant
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:

ant
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