How to join strings in Elixir?
Categories:
Mastering String Concatenation in Elixir

Explore various methods for joining strings in Elixir, from basic concatenation to efficient interpolation and binary manipulation, with practical examples.
Joining strings is a fundamental operation in any programming language, and Elixir offers several idiomatic and efficient ways to achieve this. Understanding these methods is crucial for writing clean, performant, and readable Elixir code. This article will guide you through the most common techniques, highlighting their use cases and best practices.
The Basics: Binary Concatenation (<>
)
The most straightforward and commonly used operator for joining strings in Elixir is the binary concatenation operator, <>
. In Elixir, strings are UTF-8 encoded binaries, and this operator efficiently appends one binary to another. It's simple, fast, and suitable for most concatenation needs.
name = "Alice"
message = "Hello, " <> name <> "!"
IO.puts(message)
# Output: Hello, Alice!
Using the binary concatenation operator <>
<>
operator is highly optimized for string concatenation in Elixir and is generally the preferred method for joining a few strings together.String Interpolation (#{}
)
For embedding expressions directly within a string literal, Elixir provides string interpolation using #{}
. This method is often more readable and concise than concatenation, especially when mixing variables with static string parts. It's particularly useful for constructing messages or dynamic content.
first_name = "John"
last_name = "Doe"
age = 30
full_message = "Name: #{first_name} #{last_name}, Age: #{age}."
IO.puts(full_message)
# Output: Name: John Doe, Age: 30.
String interpolation for embedding variables
#{}
. The result of the expression will be converted to a string before being inserted into the literal.Joining Lists of Strings with Enum.join/2
When you have a list of strings that you need to combine into a single string, Enum.join/2
is the most appropriate function. It takes a list of strings and an optional separator. This is very common when processing collections or building paths/URLs.
words = ["Elixir", "is", "awesome"]
sentence = Enum.join(words, " ")
IO.puts(sentence)
# Output: Elixir is awesome
path_segments = ["/users", "123", "profile"]
full_path = Enum.join(path_segments, "/")
IO.puts(full_path)
# Output: /users/123/profile
Using Enum.join/2
with and without a separator
flowchart TD A[List of Strings] --> B{"Enum.join/2"} B --> C{Separator?} C -->|Yes| D[Join with Separator] C -->|No| E[Join without Separator] D --> F[Single String Output] E --> F
Flowchart of Enum.join/2
operation
Performance Considerations
While all methods achieve string joining, their performance characteristics can differ, especially when dealing with a large number of concatenations or very long strings. For most common scenarios, the differences are negligible, but it's good to be aware of them.
<>
(Binary Concatenation): Very efficient for a small, fixed number of concatenations. Elixir's VM optimizes this well.#{}
(String Interpolation): Often compiles down to efficient binary concatenations. Highly recommended for readability.Enum.join/2
: Optimized for joining lists of strings. It's generally more efficient than repeatedly using<>
in a loop for many strings, as it can pre-allocate memory more effectively.
<>
for a large number of iterations, as this can lead to performance degradation due to frequent memory reallocations. Enum.join/2
is usually better for such cases.