How to split string across new lines and keep blank lines?

前端 未结 3 890
梦谈多话
梦谈多话 2021-02-02 07:28

Given the ruby code:

\"aaaa\\nbbbb\\n\\n\".split(/\\n/)

This outputs:

[\"aaaa\", \"bbbb\"] 

I would like the

3条回答
  •  广开言路
    2021-02-02 07:46

    You can supply a negative argument for the second parameter of split to avoid stripping trailing empty strings;

    "aaaa\nbbbb\n\n".split(/\n/, -1)
    

    Note that this will give you one extra empty string compared to what you want.

提交回复
热议问题