Vigdís — LFE Friday, LiffyBot studying Erlang modules in a Mead-style command room

LFE Friday - string:join/2

Heads up: This post was written for LFE 0.9 / Erlang/OTP 17. The current release is LFE 2.2.0. APIs and examples may be outdated.

This week's LFE Friday was translated with permission from the Erlang Thursday series by Steven Proctor. This week's translator: Robert Virding.

Today's LFE Friday is on string:join/2.

string:join/2 takes a list of strings as its first argument, and a string separator used to join the strings together into a single string.

> (string:join '("a" "b" "c") "")
"abc"
> (string:join '("a" "b" "c") "-")
"a-b-c"

The separator string can be a string of any length, and doesn't just have to be a single character.

> (string:join '("a" "b" "c") "___")
"a___b___c"
> (string:join '("a" "b" "c") " ")  
"a b c"

And as with any string, a list of characters, or even integers, can be used as the separator string.

> (string:join '("a" "b" "c") '(#\A))
"aAbAc"
> (string:join '("a" "b" "c") '(52)) 
"a4b4c"

-Proctor, Robert