如何使用 to_i 获取 Ruby 字符串中的第二个整数? [英] How do I get the second integer in a Ruby string with to_i?

查看:68
本文介绍了如何使用 to_i 获取 Ruby 字符串中的第二个整数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要转换一些字符串,并取出前两个整数,例如:

I need to convert some strings, and pull out the two first integers e.g:

unkowntext60moreunknowntext25something

unkowntext60moreunknowntext25something

致:

@width = 60
@height = 25

如果我执行 string.to_i,我会得到第一个整数:60.我不知道如何得到第二个整数 25.有什么想法吗?

If I do string.to_i, I get the first integer:, 60. I can't figure out how I get the second integer, 25. Any ideas?

推荐答案

怎么样:

text = "unkowntext60moreunknowntext25something"
@width, @height = text.scan(/\d+/).map { |n| n.to_i }  #=> 60, 25

这篇关于如何使用 to_i 获取 Ruby 字符串中的第二个整数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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