我有这个字符串:This is my example #foo and #bar
这个数组:['#foo','#bar']
这是我想要的字符串:This is my example and
THX!
答案 0 :(得分:1)
定义你的字符串和数组:
s = "This is my example #foo and #bar"
a = ['#foo', '#bar']
然后:
answer_array = (s.split(" ") - a).join(" ")
给出答案:
=> "This is my example and"
答案 1 :(得分:0)
以下保留空白:
str = 'This is my example #foo and #bar'
arr = ['#foo','#bar']
r = Regexp.new arr.each_with_object('') { |s,str| str << s << '|' }[0..-2]
#=> /#foo|#bar/
str.gsub(r,'')
#=> "This is my example and "
另一个例子(列标签):
str = " Name Age Ht Wt"
arr = [' Age', ' Wt']
r = Regexp.new arr.each_with_object('') { |s,str| str << s << '|' }[0..-2]
#=> / Age| Wt/
str.gsub(r,'')
#=> " Name Ht"