2

I know I can easily remove a substring from a string.

Now I need to remove every substring from a string, if the substring is in an array.

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"

All of the functions to remove adjust a string, such as sub, gsub, tr, ... only take one word as an argument, not an array. But my array has over 20 elements, so I need a better way than using sub 20 times.

Sadly it's not only about removing words, rather about removing the whole substring as 1. foo

How would I attempt this?

Community
  • 1
  • 1
davegson
  • 8,205
  • 4
  • 51
  • 71

3 Answers3

6

You can use gsub which accepts a regex, and combine it with Regexp.union:

string.gsub(Regexp.union(arr), '')
# => "Only delete the  and the "
Uri Agassi
  • 36,848
  • 14
  • 76
  • 93
5

Like follows:

 arr = ["1. foo", "2. bar"]
 string = "Only delete the 1. foo and the 2. bar"

 arr.each {|x| string.slice!(x) }
 string # => "Only delete the  and the " 

One extended thing, this also allows you to crop text with regexp service chars like \, or . (Uri's answer also allows):

 string = "Only delete the 1. foo and the 2. bar and \\...."
 arr = ["1. foo", "2. bar", "\..."]

 arr.each {|x| string.slice!(x) }
 string # => "Only delete the  and the  and ."
Малъ Скрылевъ
  • 16,187
  • 5
  • 56
  • 69
  • The dice decided to accept you ;) Thanks y'all for your help! – davegson Nov 28 '14 at 19:15
  • Note that if `string = "Only delete the 1. foo and the 2. bar and another 2. bar?"`, we get => `"Only delete the and the and another 2. bar?"` Is that's what's wanted? I don't know. Champ, you may wish to clarify. – Cary Swoveland Nov 28 '14 at 22:30
1

Use #gsub with #join on the array elements

You can use #gsub by calling #join on the elements of the array, joining them with the regex alternation operator. For example:

arr = ["foo", "bar"]
string = "Only delete the foo and the bar"
string.gsub /#{arr.join ?|}/, ''
#=> "Only delete the  and the "

You can then deal with the extra spaces left behind in any way you see fit. This is a better method when you want to censor words. For example:

string.gsub /#{arr.join ?|}/, '<bleep>'
#=> "Only delete the <bleep> and the <bleep>"

On the other hand, split/reject/join might be a better method chain if you need to care about whitespace. There's always more than one way to do something, and your mileage may vary.

Todd A. Jacobs
  • 81,402
  • 15
  • 141
  • 199