core :: Collection :: join
separator
.Only concatenate if separator == null
.
assert [1, 2, 3].join(":") == "1:2:3"
assert [1..3].join(":") == "1:2:3"
assert [1..3].join == "123"
if last_separator
is given, then it is used to separate the last element.
assert [1, 2, 3, 4].join(", ", " and ") == "1, 2, 3 and 4"
# Concatenate and separate each elements with `separator`.
#
# Only concatenate if `separator == null`.
#
# ~~~
# assert [1, 2, 3].join(":") == "1:2:3"
# assert [1..3].join(":") == "1:2:3"
# assert [1..3].join == "123"
# ~~~
#
# if `last_separator` is given, then it is used to separate the last element.
#
# ~~~
# assert [1, 2, 3, 4].join(", ", " and ") == "1, 2, 3 and 4"
# ~~~
fun join(separator: nullable Text, last_separator: nullable Text): String
do
if is_empty then return ""
var s = new Buffer # Result
# Concat first item
var i = iterator
var e = i.item
if e != null then s.append(e.to_s)
if last_separator == null then last_separator = separator
# Concat other items
i.next
while i.is_ok do
e = i.item
i.next
if i.is_ok then
if separator != null then s.append(separator)
else
if last_separator != null then s.append(last_separator)
end
if e != null then s.append(e.to_s)
end
return s.to_s
end
lib/core/text/abstract_text.nit:2370,2--2411,4