Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if a string is blank in Elixir

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)?

like image 223
Van Huy Avatar asked Dec 03 '15 10:12

Van Huy


People also ask

How do I know if my string is empty elixir?

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.

How can you tell if something is a string in Elixir?

Strings in Elixir are inserted between double quotes, and they are encoded in UTF-8.


2 Answers

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.

like image 167
Jason Harrison Avatar answered Oct 19 '22 23:10

Jason Harrison


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

like image 25
Gazler Avatar answered Oct 19 '22 21:10

Gazler