Ruby multiple string replacement

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

Expected output is:

\"Hello:) World:(\"

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

7条回答
  •  伪装坚强ぢ
    2020-12-02 07:54

    Late to the party but if you wanted to replace certain chars with one, you could use a regex

    string_to_replace.gsub(/_|,| /, '-')
    

    In this example, gsub is replacing underscores(_), commas (,) or ( ) with a dash (-)

提交回复
热议问题