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

时间:2022-09-15 16:01:18

How do I use string.tr to replace double quotes with single ones in Ruby?

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

2 个解决方案

#1


20  

'abc "def" ghi'.tr('"', "'")  # => abc 'def' ghi

#2


3  

Besides tr, you can also use gsub

除了tr,你也可以使用gsub

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

#1


20  

'abc "def" ghi'.tr('"', "'")  # => abc 'def' ghi

#2


3  

Besides tr, you can also use gsub

除了tr,你也可以使用gsub

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