Skip to content

String.concat ​

ant
func String.concat(parts: Array[string]): string

String.concat returns the strings in parts joined with nothing between them.

Details ​

  • Takes one array. To join two strings, use +.

Basic examples ​

Join three strings:

ant
String.concat(["ant", "hill", "s"]) // "anthills"

Nothing goes between the parts. To put a separator between them, use String.join:

ant
String.concat(["a", "b", "c"])    // "abc"
String.join(["a", "b", "c"], ",") // "a,b,c"

Scope ​

An empty array gives the empty string, and empty parts add nothing:

ant
let none: Array[string] = []
String.concat(none)          // ""
String.concat(["", "x", ""]) // "x"
String.concat(["", ""])      // ""

Parts in any script join the same way:

ant
String.concat(["é", "日本", "🐜"]) // "é日本🐜"

A part may be a combining mark, which joins the character before it into one grapheme of two code points:

ant
let s = String.concat(["e", "\u{301}"])
s.length()         // 2
len(s.graphemes()) // 1

Pass the parts with a pipe, which puts the array in as the first argument:

ant
["a", "b"] | String.concat() // "ab"

Applications ​

Join digits into one string:

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

Collect pieces in an array as a loop goes, then join them once:

ant
let parts: Array[string] = []
for i in Array.range(0, 3) {
  parts.push("${i};")
}
println(String.concat(parts))
text
0;1;2;

Build a banner from repeated pieces:

ant
func banner(title: string): string {
  let rule = "=".repeat(3)
  String.concat([rule, " ", title, " ", rule])
}

println(banner("ant"))
text
=== ant ===

Properties and relations ​

It is String.join with an empty separator:

ant
let parts = ["a", "bc", "", "d"]
assert(String.concat(parts) == String.join(parts, ""))

For two parts, it is +:

ant
let a = "ant"
let b = "hill"
assert(String.concat([a, b]) == a + b)

The length of the result is the sum of the lengths of the parts:

ant
let parts = ["é", "日本", "", "🐜"]
let total = parts.map({ x.length() }).sum() ?? 0
assert(String.concat(parts).length() == total)

It undoes String.chars and String.graphemes:

ant
let s = "héllo 日本"
assert(String.concat(s.chars()) == s)
assert(String.concat(s.graphemes()) == s)

It is associative: joining joined parts gives the same string as joining them all at once:

ant
let a = ["a", "b"]
let b = ["c"]
assert(String.concat([String.concat(a), String.concat(b)]) == String.concat([...a, ...b]))

Possible issues ​

parts.concat() reaches Array.concat, which joins two arrays, so the call is a compile error. Call String.concat(parts) or use a pipe:

ant
let s = ["a", "b"].concat() // error: Array.concat needs an argument for ys

The parts go in one array. Strings passed as separate arguments are a compile error:

ant
let s = String.concat("a", "b") // error: String.concat takes 1 argument, got 2

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

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

A part that may be null is a compile error too. Supply a fallback with ??:

ant
let x: string | null = null
let s = String.concat(["a", x]) // error: cannot use string | null where string is expected

The result is not normalized. An e joined with a combining accent looks like é, but it is not equal to the one-code-point é, U+00E9, until String.normalize brings both to one form:

ant
let s = String.concat(["e", "\u{301}"])
s == "\u{e9}"                          // false
String.normalize(s, "NFC") == "\u{e9}" // true

Reversing by code points moves a combining mark onto another character. Reverse the graphemes instead:

ant
let s = "ne\u{301}"
s.toCodePoints()                                                  // [110, 101, 769]
s.chars().reverse() | String.concat() | String.toCodePoints()     // [769, 101, 110]
s.graphemes().reverse() | String.concat() | String.toCodePoints() // [101, 769, 110]

See also ​

String.join, String.split, String.repeat, String.chars, String.graphemes, String.from, Array.concat, Bytes.concat