Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Elixir/Erlang split bitstring on newlines?

Is there a way to split a bitstring loaded from a file on newlines? I have something like this:

A line of text
Additional line of text
And another line

And I want an array like this:

["A line of text",
"Additional line of text",
"And another line"]

Is there a function to split the text on newlines to produce something like this array?

Thanks in advance.

like image 590
Stratus3D Avatar asked Nov 06 '13 22:11

Stratus3D


2 Answers

In addition to Roberts answer.

In Elixir you can use: String.split(string, "\n") Look at String module.

like image 85
couchemar Avatar answered Sep 19 '22 23:09

couchemar


If you simply split a string on \n, there are some serious portability problems. This is because many systems use \n, a few such as older macs use \r and Windows uses \r\n to delimit new lines.

The safer way to do it would be to use a regex to match any of the three above possibilities:String.split(str, ~r{(\r\n|\r|\n)}.

like image 41
Mark Wilbur Avatar answered Sep 18 '22 23:09

Mark Wilbur