Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
私はこの文字列を持っています
s = "03:23 PM on 09/04/12"
「 on 」を取り出して、スペース「 」だけに置き換えたいと思います。文字列の gsub メソッドと正規表現が最善の解決策になると思いました。なぜこれが機能しないのかわかりません。
s ="03:23 PM on 09/04/12" s.gsub(/ on /, ' ') puts s #=> "03:23 PM on 09/04/12"
文字列のgsubメソッドは、変更された文字列を返し、オブジェクト文字列をそのままにします。オブジェクトをその場で変更する場合は、 を使用する必要がありますgsub!。
gsub
gsub!
また、1 つのオカレンスのみを変更する場合subは、おそらく最善の策です。
sub
そう
s = "03:23 PM on 09/04/12" s = s.sub(' on ', ' ')
また
s.sub!(' on', ' ')