Given a string like:
s = "G o o d\r\nDay\r\n\r\n\r\nStack\r\n\r\nOverflow\r\n"
I would like to:
-
Split it by
(\r\n)+, i.e. I would like to get:["G o o d", "Day", "Stack", "Overflow"]I tried
s.split(/(\r\n)+/)but it doesn’t give me the expected result.Why ? How could I get the expected result ?
-
Get the number of
\r\nin array, i.e. the expected result is: [1, 3, 2]How would you do this ?
I use Ruby 1.9.2.
Almost, try this:
This gives
[1,3,2,1]which is possibly the “real” answer. But otherwise,s.chomp.scan...would give[1,3,2].