12

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.

5 Answers 5

18

In addition to Roberts answer.

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

10

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

1
  • You can also use re but this is overkill in this simple case.
    – rvirding
    Nov 7, 2013 at 9:14
5

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

2
  • 2
    While the idea is correct, there is an extra backslash in your regex, before the first | character. This is the correct version: String.split("foo\r\nbar", ~r{\r\n|\r|\n})
    – nietaki
    Apr 21, 2018 at 15:11
  • Right you are, I've edited the answer to remove the typo. Thanks! Oct 31, 2018 at 21:32
4

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"]
2

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.