my input string = “‘DM,US:1210’,null,1007,2021-01-11”
expected output is = [“DM,US:1210”,“null”,“1007”,“2021-01-11”]
how can i get this output ?
my input string = “‘DM,US:1210’,null,1007,2021-01-11”
expected output is = [“DM,US:1210”,“null”,“1007”,“2021-01-11”]
how can i get this output ?
There may be a way to do this in one step, but I’m no wizard…
Also, I am making several assumptions about your input, since you didn’t provide context. That said, one way to accomplish this is to split on a regex that matches either a quoted subsection or a comma and then reject the empty string members from the result.
input_string = "'DM,US:12100',null,1007,2021-01-11"
input_string.split(/'([^']*)'|,/).reject(&:empty?)
#=> ["DM,US:12100", "null", "1007", "2021-01-11"]
It turns out you can use the CSV library, but specify the single quote for the quote character:
2.7.2 :001 > require "csv"
=> true
2.7.2 :002 > input = "'DM,US:1210',null,1007,2021-01-11"
=> "'DM,US:1210',null,1007,2021-01-11"
2.7.2 :003 > CSV.new(input, :quote_char=>"'").read.first
=> ["DM,US:1210", "null", "1007", "2021-01-11"]
Thank you so much a lot!! it worked me
Thank you so much!! your solution worked for me
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.
Sponsor our Newsletter | Privacy Policy | Terms of Service | Remote Ruby Jobs