Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Ruby中使用string.tr将双引号替换为单引号?_Ruby - Fatal编程技术网

如何在Ruby中使用string.tr将双引号替换为单引号?

如何在Ruby中使用string.tr将双引号替换为单引号?,ruby,Ruby,如何在Ruby中使用string.tr将双引号替换为单引号?除了tr,您还可以使用gsub 'abc "def" ghi'.tr('"', "'") # => abc 'def' ghi irb(main):001:0> 'abc "def" ghi'.gsub(/"/,"'") => "abc 'def' ghi" tr更可取,因为gsub要慢得多:啊,总是很难找到简单的解决方案。

如何在Ruby中使用string.tr将双引号替换为单引号?

除了
tr
,您还可以使用
gsub

'abc "def" ghi'.tr('"', "'")  # => abc 'def' ghi
irb(main):001:0> 'abc "def" ghi'.gsub(/"/,"'")
=> "abc 'def' ghi"

tr
更可取,因为
gsub
要慢得多:啊,总是很难找到简单的解决方案。