Question

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.

Was it helpful?

Solution

In addition to Roberts answer.

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

OTHER TIPS

Look at binary:split/2/3 in the module binary. For example with binary:split(String, <<"\n">>).

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)}.

While Mark is right about the portability problems, the regex he provided has a typo in it and as a result doesn't work for \r\n sequences. Here's a simpler version that handles all 3 cases:

iex(13)> String.split("foo\nbar", ~r/\R/)
["foo", "bar"]
iex(14)> String.split("foo\rbar", ~r/\R/)
["foo", "bar"]
iex(15)> String.split("foo\r\nbar", ~r/\R/)
["foo", "bar"]

I recently run into a situation where the solution in my other answer and basically any other solution depending on regular expressions was in some situations much slower than depending on binary split, especially when limiting the amount of parts the string gets split into. You can see https://github.com/CrowdHailer/server_sent_event.ex/pull/11 for a more detailed analysis and a benchmark.

You can use :binary.split/3 even when targeting different types of new line characters:

iex(1)> "aaa\rbbb\nccc\r\nddd" |> :binary.split(["\r", "\n", "\r\n"], [:global])     
["aaa", "bbb", "ccc", "ddd"]

As you can see in the above example the match is greedy and \r\n takes precedence above splitting by \r first and then \n.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top