在
Python中,我们可以使用字符串的
.strip()
方法来删除所选字符的前导或尾部出现:
>>> print " (Removes (only) leading & trailing brackets & ws ) ".strip(" ()") 'Removes (only) leading & trailing brackets & ws'
解决方法
在Ruby中没有这样的方法,但是您可以轻松定义它:
def my_strip(string,chars) chars = Regexp.escape(chars) string.gsub(/\A[#{chars}]+|[#{chars}]+\z/,"") end my_strip " [la[]la] "," []" #=> "la[]la"