如何在Ruby中拆分字符串并获取除第一个之外的所有项目? [英] How to split a string in Ruby and get all items except the first one?

查看:37
本文介绍了如何在Ruby中拆分字符串并获取除第一个之外的所有项目?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

字符串是ex="test1, test2, test3, test4, test5"

当我使用

ex.split(",").first

它回来了

"test1"

现在我想获取剩余的项目,即`"test2, test3, test4, test5".如果我使用

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

ex.split(",").last

它只返回

"test5"

如何让所有剩余的项目跳过第一个?

How to get all the remaining items skipping first one?

推荐答案

试试这个:

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

现在 first 将是第一个值,rest 将是数组的其余部分.

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

这篇关于如何在Ruby中拆分字符串并获取除第一个之外的所有项目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆