How do I split string such as:
aaaaa
bbbb
aaaaa
ccccccc
aaa
rrrrt
into an array using blank lines as delimiter?
Well, with String#split
'aaaaa bbbb'.split
=> ["aaaaa", "bbbb"]
split(pattern=nil, [limit]) → an_array
Divides str into substrings based on a delimiter, returning an array of these substrings.
[...]
If
pattern
isnil
, the value of$
; is used. If$
; isnil
(which is the default),str
is split on whitespace as if' '
were specified.
UPDATE:
To split on empty line, you can use /\n{2,}/
pattern. It also handles paragraphs separated with more than one empty line:
a = <<END
aaaaa
bbbb
aaaaa
ccccccc
aaa
rrrrt
END
a.split(/\n{2,}/)
=> ["aaaaa\nbbbb", "aaaaa\nccccccc", "aaa\nrrrrt\n"]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With