Working with Strings in Elixir

One of the most basic types of just about any programming language is the String type. However, Elixir does not actually have a dedicated String type.

Instead Strings are represented as Binaries or Character Lists.

In today’s tutorial we’re going to be looking at working with Strings in Elixir.

If you want to follow along with today’s tutorial you can fire up iex (as we saw in Getting started with Elixir) so you can type the Elixir expression and see the result for yourself.

Using Strings in Elixir

Before we get into the details of how Strings are implemented in Elixir, let’s take a look at how they are used.

We’ve already seen one example of using Strings in Getting started with Elixir when we printed "Hello World" to the screen:

IO.puts "Hello World"  

In this example we’re passing the String "Hello World" to the puts function.

Strings also support interpolation in much the same way that it works in Ruby:

name = "Philip"  
"Hello #{name}"  

And you can add line breaks as you would normally expect:

"Hello\nWorld"  

There is a second way you can create a String in Elixir:

~s(Hello World)  

This is known as a Sigil and will return the same result as if you had used the first method.

So as you can see, Strings work in basically the same way in Elixir as you would expect in just about any other programming language.

Strings are Binaries

In Elixir, Strings are actually represented as Binaries. A Binary is simple a sequence of bytes.

You can check this for yourself by using the is_binary function:

is_binary("Hello World")  
true  

Due to the fact that Strings are Binaries, you can concatenate them using ``:

"Hello" <> " " <> "World"  

The String Module

The Elixir Standard Library ships with a String Module that has a number of useful functions that can be used on Strings. Here are a couple of highlights:

String.capitalize("hello world")

String.length("Hello World")

String.match?("Hello World", ~r/Hello/)

String.replace("Hello World", " ", "-")  

I won’t go into any more detail than that as you really don’t need to memorise all of the functions of the String Module.

What are Character Lists?

In the introduction to this post I mentioned the fact that Strings are represented as Binaries and Character Lists.

We’ve already looked at Binaries, so what are Character Lists?

A Character List is basically the same, but you using single quotes instead of double quotes:

‘Hello World’  

You can check that this is not a Binary with the is_binary function:

is_binary(‘Hello World’)  
false  

A Character List is simply a list of integers that represent each character of the String.
– You can see this for yourself if you try the following:

[88, 89, 90]  

This will return 'XYZ' as iex does not distinguish between the String representation and the Character List.

However, Strings created with single quotes are not interchangeable with Strings created with double quotes. If you pass the wrong type to a function you will get an error.

So why is there a distinction between using single quotes and double quotes?

For the most part, Elixir code will use Binaries and so you should too. Character Lists are how Erlang has historically represented Strings and so if you are using an Erlang third-party library you may encounter them.

However, it’s pretty easy to switch between Binaries and Character Lists or vice-versa.

To convert a Binary to a Character List:

String.to_char_list("Hello World")  

Or to convert a Character List to a Binary:

List.to_string(‘Hello World’)  

As a side note, you can also use interpolation or Sigils with Character Lists.

Interpolation works as you would expect it to:

name = ‘Philip’  
‘Hello #{name}’  

To use Sigils with Character Lists, use the ~c() syntax:

~c(Hello World)  

What are Heredocs?

Finally we have one last thing to mention when it comes to working with Strings in Elixir. Heredocs are multiline Strings that are mostly used for documentation of code:

"""  
Hello World  
"""  

You can also use single quotes to create a Character List Heredoc if you want, but you will almost exclusively see them created as Binaries:

”’  
Hello World  
”’  

Conclusion

If you are coming from a programming language like Ruby, there probably wasn’t a lot here that was a revelation to you.

Strings are one of the basic types of a programming language, and so there isn’t any great surprises with Elixir.

One thing to be aware of is the difference between Binary Strings and Character Lists.

You should be using Binary Strings as these are more like what you would encounter in other programming languages.

But you might encounter Character Lists if you are using an Erlang library.

So having that understanding of the difference between the two types of Strings will probably save you an inevitable headache at some point in the future!

Working with Strings in Elixir
Share this