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

前端 未结 3 878
梦谈多话
梦谈多话 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:51

    You can use the numeric argument, but IMO it's a bit tricky since (IMO) it's not quite consistent with what I'd expect, and AFAICT you'd want to trim the last null field:

    jruby-1.6.7 :020 > "aaaa\nbbbb\n\n".split(/\n/, -1)[0..-2]
     => ["aaaa", "bbbb", ""] 
    

提交回复
热议问题