How to strip leading and trailing quote from string, in Ruby

后端 未结 9 1500
栀梦
栀梦 2020-12-17 07:59

I want to strip leading and trailing quotes, in Ruby, from a string. The quote character will occur 0 or 1 time. For example, all of the following should be converted to f

9条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-17 08:20

    I wanted the same but for slashes in url path, which can be /test/test/test/ (so that it has the stripping characters in the middle) and eventually came up with something like this to avoid regexps:

    '/test/test/test/'.split('/').reject(|i| i.empty?).join('/')
    

    Which in this case translates obviously to:

     '"foo,bar"'.split('"').select{|i| i != ""}.join('"')
    

    or

    '"foo,bar"'.split('"').reject{|i| i.empty?}.join('"')
    

提交回复
热议问题