Ruby multiple string replacement

后端 未结 7 1267
遇见更好的自我
遇见更好的自我 2020-12-02 07:17
str = \"Hello☺ World☹\"

Expected output is:

\"Hello:) World:(\"

I can do this: str.gsub(\"☺\", \":)\").gsu

7条回答
  •  感动是毒
    2020-12-02 08:01

    Another simple way, and yet easy to read is the following:

    str = '12 ene 2013'
    map = {'ene' => 'jan', 'abr'=>'apr', 'dic'=>'dec'}
    map.each {|k,v| str.sub!(k,v)}
    puts str # '12 jan 2013'
    

提交回复
热议问题