【发布时间】:2014-11-29 03:14:37
【问题描述】:
我知道我可以轻松remove a substring from a string。
现在我需要从字符串中删除每个子字符串,如果子字符串在数组中。
arr = ["1. foo", "2. bar"]
string = "Only delete the 1. foo and the 2. bar"
# some awesome function
string = string.replace_if_in?(arr, '')
# desired output => "Only delete the and the"
所有的删除函数都可以调整字符串,例如sub、gsub、tr,...只需要一个单词作为参数,而不是数组。但是我的数组有超过 20 个元素,所以我需要一个比使用 sub 20 次更好的方法。
遗憾的是,这不仅仅是删除单词,而是删除整个子字符串为1. foo
我将如何尝试?
【问题讨论】: