从字符串中删除双引号 [英] Remove double quotes from string

查看:68
本文介绍了从字符串中删除双引号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从 MySQL 数据库中获取数据并使用 Ruby 将其重新格式化为纯文本文件.我的一些 MySQL 数据包含双引号,如下所示:

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:

马修马特"佩里

我需要删除那些引号并用其他东西替换它们,|例如.

and I need to remove those quotes and replace them with something else, | for instance.

我在 stackoverflow 上发现了另一篇关于删除建议如下的引号的帖子:

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

马修|马特|佩里

相反?

推荐答案

如果你不想修改s,这里就可以了:

This will do it if you don't want to modify s:

new_s = s.gsub /"/, '|'

如果你想修改s:

s.gsub! /"/, '|'

这篇关于从字符串中删除双引号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆