How to join a list of strings in Elixir

Overview

The join() method of the Enum module is used to join the given enumerable to a string with a separator.

Syntax

Enum.join(enumerable, joiner \\ "")

Parameters

  • enumerable: The enumerable/list of strings.
  • joiner: The separator string used for joining the strings.

Return value

The method returns a string.

Code

lst = ["hello", "educative", "answers"]
joiner = " -- "
joined_string = Enum.join(lst, joiner)
IO.inspect lst
IO.puts "separator: #{joiner}"
IO.puts "--------"
IO.puts "Joined string from the list -"
IO.puts joined_string

Explanation

  • Line 3: We join the list of strings into a string based on the given separator using the Enum.join() method.
  • Line 8: We print the string returned after the join.

Free Resources

Copyright ©2025 Educative, Inc. All rights reserved