The Truth About Elixir Strings — Tentamen Software Testing Blog

Image for post
Image for post
Elixir Strings As Binaries

TL;DR

No Dedicated String Type

Binary Strings (Double Quotes)

Image for post
Image for post
Elixir String sigils

Sigils are useful ~s when you have " as part of your String. It handles your error-prone character escaping. Uppercase is used for not interpreting embedded expression and not escaping characters. Heredoc is also a multiline string, and it is used for modules and function documentation. In the end, we must note the essential side effects of String binaries. Strings are concatenated in the same way as Binaries, using the <> operator.

Character Lists (Single Quotes)

Image for post
Image for post
Elixir String As Character List

String as a character list is defined using single quotes. We use single quotes to define character list strings. You can see that presents character list string as a list of integers. Each integer is a decimal encoding value (encoding set on your computer, in this case, this if UTF) for that character.

Binary Or Character List

List Module

Image for post
Image for post
Elixir List Module

The list module works with Binary Strings.

Remember

  • How to use binaries for Strings
  • Embedded string expressions #{} (as in Ruby)
  • How to escape characters
  • Multiline String
  • When to use sigils, ~s() both uppercase and lowercase
  • Heredocs strings
  • String concatenation
  • How to use lists for strings
  • " and ' difference
  • When to use ~c sigils
  • Prefer Binary Strings over Character Lists
  • Use the List Module function, do not reinvent the wheel.

Originally published at https://blog.tentamen.eu on September 4, 2020.

Written by

Founder of Tentamen, software testing agency.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store