How to check if a string is blank in Elixir

25,067

Solution 1

String.trim/1 seems to do the trick as of Elixir 1.3.0.

strip still works, but it was soft deprecated in the 1.3.0 release and it isn't listed in the String docs.

Solution 2

There is String.strip/1 which will convert your 3 examples to "" which you can compare against.

iex(4)> String.strip("\n") == ""
true
iex(5)> String.strip("") == ""  
true
iex(6)> String.strip("    ") == ""
true

There was an issue about it https://github.com/elixir-lang/elixir/pull/2707

Solution 3

why not just use pattern matching

iex> a = ""
""
iex> b = "b"
"b"
iex> ^b = "b"
"b"
iex> ^a = "your String"
** (MatchError) no match of right hand side value: ""
iex> ^a = ""
""

or better yet check its byte size

iex> if byte_size("") == 0 do true else false end
true
iex> if byte_size("a") == 0 do true else false end
false

Solution 4

I published a tiny library to do this properly for any data type. It implements identical behaviour as Rails' blank? method in Elixir as far as that's possible.

Library is here: https://github.com/samphilipd/blankable

To install, add blankable to your list of dependencies in mix.exs:

def deps do
  [{:blankable, "~> 0.0.1"}]
end

Solution 5

I had the same question today. I ended up defining these function:

defmodule Hello do 
  def is_blank(nil), do: true
  def is_blank(val) when val == %{}, do: true
  def is_blank(val) when val == [], do: true
  def is_blank(val) when is_binary(val), do: String.trim(val) == ""
  def is_blank(val), do: false
end

import Hello 

is_blank nil 
is_blank [] 
is_blank %{} 
is_blank "" 

is_blank ["A"] 
is_blank %{a: "A"} 
is_blank "A" 
Share:
25,067
Van Huy
Author by

Van Huy

Updated on September 09, 2021

Comments

  • Van Huy
    Van Huy over 2 years

    I mean a string is blank if it's empty or contains whitespaces only. For example, "", " " and "\n" are all blank.

    In Rails, we have the .blank? method.

    Is there something similar in Elixir (or in the Phoenix Framework)?

  • ichigolas
    ichigolas almost 8 years
    What if I want to to do that inside a guard?
  • stevendaniels
    stevendaniels almost 8 years
    strip has been soft-deprecated in Elixir > 1.3.0. github.com/elixir-lang/elixir/releases/tag/v1.3.0
  • Chris Keele
    Chris Keele over 7 years
    @nicooga if you just want to check for empty strings inside guard, try: when binary_part(string, 1, -1)
  • David S.
    David S. over 6 years
    I only just started learning Elixir, but even as a newbie I find pattern matching to be the idiomatic way.
  • sunofkyuss
    sunofkyuss about 6 years
    @Chris Keele that does not work for any of the cases.
  • Pierre Pretorius
    Pierre Pretorius over 3 years
    Doesn't work for nil like in Rails so a more verbose solution is required. This will throw error: String.trim(nil)
  • LowFieldTheory
    LowFieldTheory over 3 years
    the third one will match any map.
  • mnishiguchi
    mnishiguchi over 3 years
    @LowFieldTheory You are absolutely right. I fix my code.
  • tblev
    tblev almost 3 years
    Pattern Matching with strings isn't recommended.
  • mjwrazor
    mjwrazor almost 3 years
    @tblev for single character matching I believe it is okay. It doesn't look like it is recommended for strings of unknown length.