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

RubyStringSplit

Ruby Problem Overview


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?

Ruby Solutions


Solution 1 - Ruby

Try this:

first, *rest = ex.split(/, /)

Now first will be the first value, rest will be the rest of the array.

Solution 2 - Ruby

ex.split(',', 2).last

The 2 at the end says: split into 2 pieces, not more.

normally split will cut the value into as many pieces as it can, using a second value you can limit how many pieces you will get. Using ex.split(',', 2) will give you:

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

as an array, instead of:

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

Solution 3 - Ruby

Since you've got an array, what you really want is Array#slice, not split.

rest = ex.slice(1 .. -1)
# or
rest = ex[1 .. -1]

Solution 4 - Ruby

You probably mistyped a few things. From what I gather, you start with a string such as:

string = "test1, test2, test3, test4, test5"

Then you want to split it to keep only the significant substrings:

array = string.split(/, /)

And in the end you only need all the elements excluding the first one:

# We extract and remove the first element from array
first_element = array.shift

# Now array contains the expected result, you can check it with
puts array.inspect

Did that answer your question ?

Solution 5 - Ruby

Sorry a bit late to the party and a bit surprised that nobody mentioned the drop method:

ex="test1, test2, test3, test4, test5"
ex.split(",").drop(1).join(",")
=> "test2,test3,test4,test5"

Solution 6 - Ruby

ex="test1,test2,test3,test4,test5"
all_but_first=ex.split(/,/)[1..-1]

Solution 7 - Ruby

if u want to use them as an array u already knew, else u can use every one of them as a different parameter ... try this :

parameter1,parameter2,parameter3,parameter4,parameter5 = ex.split(",")

Solution 8 - Ruby

You can also do this:

String is ex="test1, test2, test3, test4, test5"
array = ex.split(/,/)
array.size.times do |i|
  p array[i]
end 

Solution 9 - Ruby

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionsgiView Question on Stackoverflow
Solution 1 - RubyavdgaagView Answer on Stackoverflow
Solution 2 - Rubyuser163365View Answer on Stackoverflow
Solution 3 - RubyKonrad RudolphView Answer on Stackoverflow
Solution 4 - RubybltxdView Answer on Stackoverflow
Solution 5 - RubyAnand ShahView Answer on Stackoverflow
Solution 6 - RubyJonas ElfströmView Answer on Stackoverflow
Solution 7 - RubyRaafatView Answer on Stackoverflow
Solution 8 - RubyG GillView Answer on Stackoverflow
Solution 9 - RubylalitView Answer on Stackoverflow