Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- outcome_array=the_text.split(pattern_to_split_by)
- irb(main):015:0> s = "split on the word on okay?"
- => "split on the word on okay?"
- irb(main):016:0> b=[]; s.split(/(on)/).each_slice(2) { |s| b << s.join }; b
- => ["split on", " the word on", " okay?"]
- irb(main):007:0> "split it here and here okay".split(/ (here) /)
- => ["split it", "here", "and", "here", "okay"]
- s = "split on the word on and include on with previous"
- a = s.split(/(on)/)
- # iterate through and combine adjacent items together and store
- # results in a second array
- b = []
- a.each_index{ |i|
- b << a[i] if i.even?
- b[b.length - 1] += a[i] if i.odd?
- }
- print b
- ["split on", " the word on", " and include on", " with previous"]
Add Comment
Please, Sign In to add comment