String.concat
func String.concat(parts: Array[string]): stringString.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:
String.concat(["ant", "hill", "s"]) // "anthills"Nothing goes between the parts. To put a separator between them, use String.join:
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:
let none: Array[string] = []
String.concat(none) // ""
String.concat(["", "x", ""]) // "x"
String.concat(["", ""]) // ""Parts in any script join the same way:
String.concat(["é", "日本", "🐜"]) // "é日本🐜"A part may be a combining mark, which joins the character before it into one grapheme of two code points:
let s = String.concat(["e", "\u{301}"])
s.length() // 2
len(s.graphemes()) // 1Pass the parts with a pipe, which puts the array in as the first argument:
["a", "b"] | String.concat() // "ab"Applications
Join digits into one string:
[1, 2, 3].map({ String.from(x) }) | String.concat() // "123"Collect pieces in an array as a loop goes, then join them once:
let parts: Array[string] = []
for i in Array.range(0, 3) {
parts.push("${i};")
}
println(String.concat(parts))0;1;2;Build a banner from repeated pieces:
func banner(title: string): string {
let rule = "=".repeat(3)
String.concat([rule, " ", title, " ", rule])
}
println(banner("ant"))=== ant ===Properties and relations
It is String.join with an empty separator:
let parts = ["a", "bc", "", "d"]
assert(String.concat(parts) == String.join(parts, ""))For two parts, it is +:
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:
let parts = ["é", "日本", "", "🐜"]
let total = parts.map({ x.length() }).sum() ?? 0
assert(String.concat(parts).length() == total)It undoes String.chars and String.graphemes:
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:
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:
let s = ["a", "b"].concat() // error: Array.concat needs an argument for ysThe parts go in one array. Strings passed as separate arguments are a compile error:
let s = String.concat("a", "b") // error: String.concat takes 1 argument, got 2Every part must be a string. Convert other values with String.from first:
let s = String.concat(["a", 1]) // error: cannot use int where string is expectedA part that may be null is a compile error too. Supply a fallback with ??:
let x: string | null = null
let s = String.concat(["a", x]) // error: cannot use string | null where string is expectedThe 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:
let s = String.concat(["e", "\u{301}"])
s == "\u{e9}" // false
String.normalize(s, "NFC") == "\u{e9}" // trueReversing by code points moves a combining mark onto another character. Reverse the graphemes instead:
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
