如何将字符串拆分为整数数组 [英] How to split string into array as integers
本文介绍了如何将字符串拆分为整数数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
鉴于这样的事情
@grid = "4x3".split("x")
当前结果是一个字符串数组"4","3"
The current result is an array of strings "4","3"
有什么捷径可以直接拆分成整数吗?
Is there any shortcut to split it directly to integers?
推荐答案
ruby-1.9.2-p136 :001 > left, right = "4x3".split("x").map(&:to_i)
=> [4, 3]
ruby-1.9.2-p136 :002 > left
=> 4
ruby-1.9.2-p136 :003 > right
=> 3
对结果数组调用 map 以转换为整数,并将每个值分别分配给左和右.
Call map on the resulting array to convert to integers, and assign each value to left and right, respectively.
这篇关于如何将字符串拆分为整数数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文