Check if String contains any String from array

后端 未结 3 756
别那么骄傲
别那么骄傲 2021-01-21 01:47

I know I can check if a string contains another string like this

NSString *string = @\"hello bla bla\";
if ([string rangeOfString:@\"bla\"].location == NSNotFoun         


        
3条回答
  •  野性不改
    2021-01-21 02:06

    Besides my cheating the my other question, here an idea how really to avoid time costly looping: Use Set computation magic!

    • created a class 'Sentence', instantiate it with the strings to test
    • created a 'Word' class that takes a word to search for
    • overwrite both classes' isEqual: method to match for if a word is in the sentence (use sets there too!)
    • put those into an array.
    • from this array create a NS(*)Set object
    • put all word in a set
    • execute union on it.

提交回复
热议问题