简体   繁体   中英

Escape string characters

I have a string as given below:

"\"funlolx.com, Likesgag.com (269556)\",3,0,0.000000000000000000"

I want the string as below so that I can split it by "," ,

"funlolx.com Likesgag.com (269556),3,0,0.000000000000000000"

so the final output I get would be like:

["funlolx.com Likesgag.com (269556)", "3", "0", "0.000000000000000000"]
"\"funlolx.com, Likesgag.com (269556)\",3,0,0.000000000000000000"
.gsub(/"[^"]+"/){|s| s.delete('",')}
# => "funlolx.com Likesgag.com (269556),3,0,0.000000000000000000"

This looks suspiciously like CSV to me:

require 'csv'
table = CSV.parse('"funlolx.com, Likesgag.com (269556)",3,0,0.000000000000000000')
# => [["funlolx.com, Likesgag.com (269556)", "3", "0", "0.000000000000000000"]]

Note: CSV expects to get a table, ie an array of arrays. In this case, there is only one row in the table, so you want to get the first one:

table.first
# => ["funlolx.com, Likesgag.com (269556)", "3", "0", "0.000000000000000000"]

If you really must get rid of the comma, then you can use all of Ruby's standard string operations:

table.first.map {|s| s.delete(',') }
# => ["funlolx.com Likesgag.com (269556)", "3", "0", "0.000000000000000000"]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM