Remove double quotes from string
34,139
Solution 1
This will do it if you don't want to modify s
:
new_s = s.gsub /"/, '|'
If you do want to modify s
:
s.gsub! /"/, '|'
Solution 2
You could use something like:
text = 'Matthew "Matt" Perry'
text.tr(%q{"'}, '|') # => "Matthew |Matt| Perry"
text = "Matthew 'Matt' Perry"
text.tr(%q{"'}, '|') # => "Matthew |Matt| Perry"
Related videos on Youtube
Comments
-
Libby almost 2 years
I'm trying to grab data from a MySQL database and use Ruby to reformat it into a flat text file. Some of my MySQL data contains double quotes like so:
Matthew "Matt" Perry
and I need to remove those quotes and replace them with something else, | for instance.
I found another post on stackoverflow about removing quotes that suggested the following:
s.scan(/'(.+?)'|"(.+?)"|([^ ]+)/).flatten.compact
but that returns the string intact (with double quotes). How can I get
Matthew |Matt| Perry
instead?
-
Nakilon over 13 yearsSO's syntax highlighter failed again )