Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

string

split

ruby

Given the ruby code:

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

This outputs:

["aaaa", "bbbb"]  

I would like the output to include the blank line indicated by \n\n -- I want the result to be:

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

What is the easiest/best way to get this exact result?

like image 763
Kirk Woll Avatar asked Aug 21 '12 19:08

Kirk Woll


People also ask

How do you split a string with a new line?

To split a string on newlines, you can use the regular expression '\r?\ n|\r' which splits on all three '\r\n' , '\r' , and '\n' . A better solution is to use the linebreak matcher \R which matches with any Unicode linebreak sequence. You can also split a string on the system-dependent line separator string.

How do I split text in an empty line?

To split text by empty line, split the string on two newline characters, e.g. my_str. split('\n\n') for POSIX encoded files and my_str. split('\r\n\r\n') for Windows encoded files.

How do I split a string without a separator?

Q #4) How to split a string in Java without delimiter or How to split each character in Java? Answer: You just have to pass (“”) in the regEx section of the Java Split() method. This will split the entire String into individual characters.


2 Answers

I'd recommend using lines instead of split for this task. lines will retain the trailing line-break, which allows you to see the desired empty-line. Use chomp to clean up:

"aaaa\nbbbb\n\n".lines.map(&:chomp) [     [0] "aaaa",     [1] "bbbb",     [2] "" ] 

Other, more convoluted, ways of getting there are:

"aaaa\nbbbb\n\n".split(/(\n)/).each_slice(2).map{ |ary| ary.join.chomp } [     [0] "aaaa",     [1] "bbbb",     [2] "" ] 

It's taking advantage of using a capture-group in split, which returns the split text with the intervening text being split upon. each_slice then groups the elements into two-element sub-arrays. map gets each two-element sub-array, does the join followed by the chomp.

Or:

"aaaa\nbbbb\n\n".split(/(\n)/).delete_if{ |e| e == "\n" } [     [0] "aaaa",     [1] "bbbb",     [2] "" ] 

Here's what split is returning:

"aaaa\nbbbb\n\n".split(/(\n)/) [     [0] "aaaa",     [1] "\n",     [2] "bbbb",     [3] "\n",     [4] "",     [5] "\n" ] 

We don't see that used very often, but it can be useful.

like image 158
the Tin Man Avatar answered Sep 20 '22 06:09

the Tin Man


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.

like image 34
Mark Byers Avatar answered Sep 19 '22 06:09

Mark Byers