How to delete specific characters from a string in Ruby?

前端 未结 5 1833
失恋的感觉
失恋的感觉 2021-02-01 00:04

I have several strings that look like this:

\"((String1))\"

They are all different lengths. How could I remove the parentheses from all these s

5条回答
  •  南笙
    南笙 (楼主)
    2021-02-01 00:43

    Using String#gsub with regular expression:

    "((String1))".gsub(/^\(+|\)+$/, '')
    # => "String1"
    "(((((( parentheses )))".gsub(/^\(+|\)+$/, '')
    # => " parentheses "
    

    This will remove surrounding parentheses only.

    "(((((( This (is) string )))".gsub(/^\(+|\)+$/, '')
    # => " This (is) string "
    

提交回复
热议问题