How to split a string in Ruby and get all items except the first one?

Viewed 155521

String is ex="test1, test2, test3, test4, test5"

when I use

ex.split(",").first

it returns

"test1"

Now I want to get the remaining items, i.e. `"test2, test3, test4, test5". If I use

ex.split(",").last

it returns only

"test5"

How to get all the remaining items skipping first one?

9 Answers

Try split(",")[i] where i is the index in result array. split gives array below

["test1", " test2", " test3", " test4", " test5"] 

whose element can be accessed by index.

Related