在 Python 中,我可以从字符串中去除空格、换行符或随机字符,例如
>>> '/asdf/asdf'.strip('/')
'asdf/asdf' # Removes / from start
>>> '/asdf/asdf'.strip('/f')
'asdf/asd' # Removes / from start and f from end
>>> ' /asdf/asdf '.strip()
'/asdf/asdf' # Removes white space from start and end
>>> '/asdf/asdf'.strip('/as')
'df/asdf' # Removes /as from start
>>> '/asdf/asdf'.strip('/af')
'sdf/asd' # Removes /a from start and f from end
但是 Ruby 的String#strip方法不接受任何参数。我总是可以回退到使用正则表达式,但是有没有一种方法/方法可以在不使用正则表达式的情况下从 Ruby 中的字符串(前后)中去除随机字符?