My string:
我的字符串:
>> pp params[:value]
"07016,07023,07027,07033,07036,07060,07062,07063,07065,07066,07076,07081,07083,07088,07090,07092,07201,07202,07203,07204,07205,07206,07208,07901,07922,07974,08812,07061,07091,07207,07902"
How can this become an array of separate numbers like :
如何成为一组单独的数字,如:
["07016", "07023", "07033" ... ]
3 个解决方案
#1
34
result = params[:value].split(/,/)
String#split is what you need
String#split是你需要的
#3
6
Note that what you ask for is not an array of separate numbers, but an array of strings that look like numbers. As noted by others, you can get that with:
请注意,您要求的不是单独数字的数组,而是一个看起来像数字的字符串数组。正如其他人所说,你可以用:
arr = params[:value].split(',')
# Alternatively, assuming integers only
arr = params[:value].scan(/\d+/)
If you actually wanted an array of numbers (Integers), you could do it like so:
如果你真的想要一个数组(整数),你可以这样做:
arr = params[:value].split(',').map{ |s| s.to_i }
# Or, for Ruby 1.8.7+
arr = params[:value].split(',').map(&:to_i)
# Silly alternative
arr = []; params[:value].scan(/\d+/){ |s| arr << s.to_i }
#1
34
result = params[:value].split(/,/)
String#split is what you need
String#split是你需要的
#2
#3
6
Note that what you ask for is not an array of separate numbers, but an array of strings that look like numbers. As noted by others, you can get that with:
请注意,您要求的不是单独数字的数组,而是一个看起来像数字的字符串数组。正如其他人所说,你可以用:
arr = params[:value].split(',')
# Alternatively, assuming integers only
arr = params[:value].scan(/\d+/)
If you actually wanted an array of numbers (Integers), you could do it like so:
如果你真的想要一个数组(整数),你可以这样做:
arr = params[:value].split(',').map{ |s| s.to_i }
# Or, for Ruby 1.8.7+
arr = params[:value].split(',').map(&:to_i)
# Silly alternative
arr = []; params[:value].scan(/\d+/){ |s| arr << s.to_i }