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?

3 Answers 3

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 "
Sign up to request clarification or add additional context in comments.

Comments

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 ."

2 Comments

The dice decided to accept you ;) Thanks y'all for your help!
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.
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.

2 Comments

Using join is not optimal, since it does not escape the text in each element 1. foo will match 11 foo as well...
fyi: 1. foo will match 11. foo in all answers, so this answer is not inferior

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.