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

后端 未结 8 963
故里飘歌
故里飘歌 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:20

    bad = %w(foo bar baz)
    str = "This is my first foo string"
    
    # find the first word in the list
    found = bad.find {|word| str.include?(word)}
    
    # remove it
    str[found] = ''  ;# str => "This is my first  string"
    

提交回复
热议问题