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?
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.
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.
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.
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.
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! /"/, '|'
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"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With