给定一个以字符串形式存储的十进制值数组,例如["006", "025", "004", "034", "158", "019", "255", "000"]
,
如何在保留值作为字符串以获取["6", "25", "4", "34", "158", "19", "255", "0"]
?
答案 0 :(得分:2)
string = '006 025 004 034 158 019 255 000'
answer = string.split(",").map(&:to_i)
返回:
[6, 25, 4, 34, 158, 19, 255, 0]
如果你想将它展平并将其变回相同格式的字符串:
answer = answer.join(' ')
离开:
"6 25 4 34 158 19 255 0"
答案 1 :(得分:1)
"006 025 004 034 158 019 255 000".gsub(/(?<= |\A)0+(?=\d)/,'')
#=> "6 25 4 34 158 19 255 0"
正则表达式为: