2010-10-05 12 views

risposta

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

In Ruby 1.9+, è molto più chiaro per usare '... tr (?",? ') '. – jtbandes

2

Inoltre tr, è anche possibile utilizzare gsub

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

' tr' è preferibile poiché 'gsub' è molto più lento: https://github.com/JuanitoFatas/fast-ruby#stringgsub-vs-stringtr-code –

Problemi correlati