Ruby – 如何在字符串的开头和结尾添加字符
如何在字符串的开头和结尾添加撇号?
string = "1,2,3,4"
我希望那个字符串是:
'1','2','3','4'
result = [] "1,2,3,4".split(',').each do |c| result << "'#{c.match /\d+/}'" end puts result.join(',') '1','2','3','4'
不确定,如果这是你想要的:
>> s = "1,2,3,4" >> s.split(',').map { |x| "'#{x}'" }.join(',') => "'1','2','3','4'"
我们可以使用正则表达式来查找数字
string = "1,2,3,4" string.gsub(/(\d)/, '\'\1\'') #=> "'1','2','3','4'"
str.insert(0,’x’)str.insert(str.length,’x’)
看到你的编辑后。
q =“1,2,3,4” ar = q.split(',') ar.each {|我| i.insert(0,“'”)。insert(-1,“'”)} q = ar.join(',')