What's the fastest way to check if a word from one string is in another string?
Posted
by Mike Trpcic
on Stack Overflow
See other posts from Stack Overflow
or by Mike Trpcic
Published on 2010-03-31T02:51:36Z
Indexed on
2010/03/31
2:53 UTC
Read the original article
Hit count: 286
I have a string of words; let's call them bad
:
bad = "foo bar baz"
I can keep this string as a whitespace separated string, or as a list:
bad = bad.split(" ");
If I have another string, like so:
str = "This is my first foo string"
What's the fasted way to check if any word from the bad
string is within my comparison string, and what's the fastest way to remove said word if it's found?
#Find if a word is there
bad.split(" ").each do |word|
found = str.include?(word)
end
#Remove the word
bad.split(" ").each do |word|
str.gsub!(/#{word}/, "")
end
© Stack Overflow or respective owner