Elixir - Split a string so that it does not return empty strings in a list?

I split the string into regex. The resulting array contains empty lines where the regular expression matches. I do not want these. For example.

iex(1)> String.split("Hello world. How are you?", ~r/\W/) ["Hello", "world", "", "How", "are", "you", ""] 

How can I split a string so that it does not return empty strings in a list?

+7
elixir
source share
1 answer

As mentioned in the String.split docs ,

Blank lines are only removed from the result if the trim parameter is set to true (the default is false).

So you want to add this as an option to your call to String.split :

 String.split("Hello world. How are you?", ~r/\W/, trim: true) ["Hello", "world", "How", "are", "you"] 
+12
source share

All Articles