2011-03-09 104 views

回答

1
result = [] 
"1,2,3,4".split(',').each do |c| 
    result << "'#{c.match /\d+/}'" 
end 
puts result.join(',') 

'1','2','3','4' 
1

str.insert(0, 'X') str.insert(str.length, 'X')

后看到你的编辑。

 
q = "1,2,3,4" 

ar = q.split(',') 

ar.each{|i| i.insert(0, "'").insert(-1, "'")} 

q = ar.join(',') 
10

不知道,如果这是你想要的东西:

>> s = "1,2,3,4" 
>> s.split(',').map { |x| "'#{x}'" }.join(',') 
=> "'1','2','3','4'" 
2

我们可以使用正则表达式来寻找数字

string = "1,2,3,4" 
string.gsub(/(\d)/, '\'\1\'') 
#=> "'1','2','3','4'" 
+0

还是要高于9:string.gsub (/(\ d +)/,'\'\ 1 \')) – tardate 2011-10-06 12:25:25