Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove double quotes from string

Tags:

string

regex

ruby

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?

like image 654
Libby Avatar asked Jan 18 '11 04:01

Libby


People also ask

How do you remove two double quotes from a string in Java?

Escape double quotes in java Double quotes characters can be escaped with backslash( \ ) in java. You can find complete list of String escapes over this java doc. Let's understand this with the help of example.

How do you remove double quotes from an array?

Using split() and join() method To remove double quotes from String in JavaScript: Split the string by double quotes using String's split() method. Join the String with empty String using Array's join() method.

How do you remove double quotes from a string in C++?

Removing double quotes from string in C++ We can do this by using erase() function. Basically, what is the work of erase() function? It removes the elements from a vector or a string in some range.

How do you remove double quotes from a string in Python?

Using the strip() Function to Remove Double Quotes from String in Python. We use the strip() function in Python to delete characters from the start or end of the string. We can use this method to remove the quotes if they exist at the start or end of the string.


2 Answers

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! /"/, '|'
like image 108
dontangg Avatar answered Sep 23 '22 23:09

dontangg


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"
like image 39
the Tin Man Avatar answered Sep 21 '22 23:09

the Tin Man