gpt4 book ai didi

ruby - 如何使用 to_i 获取 Ruby 字符串中的第二个整数?

转载 作者:数据小太阳 更新时间:2023-10-29 08:14:34 24 4
gpt4 key购买 nike

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

unkowntext60moreunknowntext25something

收件人:

@width = 60
@height = 25

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

最佳答案

怎么样:

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

关于ruby - 如何使用 to_i 获取 Ruby 字符串中的第二个整数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1060300/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com