What's the fastest way to check if a word from one string is in another string?

后端 未结 8 952
故里飘歌
故里飘歌 2020-12-30 12:56

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,

8条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-30 13:28

    I usually make a point of not optimizing without measurements, but here's a wag:

    To make it fast, you should iterate through each string once. You want to avoid a loop with bad count * str count inner compares. So, you could build a big regexp and gsub with it.

    (adding foo variants to test word boundary works)

    str = "This is my first foo fooo ofoo string"
    
    => "This is my first foo fooo ofoo string"
    
    badex = /\b(#{bad.split.join('|')})\b/
    
    => /\b(foo|bar|baz)\b/
    
    str.gsub(badex,'').gsub('  ',' ')
    
    => "This is my first fooo ofoo string"
    

    Of course the huge resulting regexp might be as slow as the implied nested iteration in my other answer. Only way to know is to measure.

提交回复
热议问题