这里我有行
row = "2016-07-25 00:00:00 UTC,237504,dsfdf,DM12,\"BPM , Fully Auto\""
我想将其与,
row = row.split(",")
我收到了这个
["2016-07-25 00:00:00 UTC", "237504", "dsfdf", "DM12", \"BPM ", " Fully Auto\""]
有没有办法获得这个
["2016-07-25 00:00:00 UTC", "237504", "dsfdf", "DM12", "\"BPM , Fully Auto\""]
我们需要区分“\”BPM,全自动\“”
如果我们这样做,我需要在这里做一些不用逗号分割的内容 用
括起来\" \"
答案 0 :(得分:3)
String#split
使用第二个参数来限制拆分:
row.split(",", 4)
# => ["2016-07-25 00:00:00 UTC", "237504", "dsfdf", "DM12", "\"\"\"BPM , Fully Auto\"\"\""]
答案 1 :(得分:3)
该行是否来自csv文件? 也许你可以使用:
require 'csv'
CSV.parse_line(row)
# => ["2016-07-25 00:00:00 UTC", "237504", "dsfdf", "DM12", "\"BPM , Fully Auto\""]
答案 2 :(得分:0)
您还可以拆分,切片和加入必要的部分:
row = row.split(",")
row = row[0..-3] + [row[-2..-1].join(", ")]