How would you scan an array of strings for a set of substrings in objective-c?

后端 未结 5 1756
情歌与酒
情歌与酒 2021-01-27 03:21

So I basically have an array of words and phrases. Some of them contain curses. I want to create a method that automatically scans each of the units in the array for curses. If

5条回答
  •  旧巷少年郎
    2021-01-27 03:35

    I'd do two nested for-loops. The first loop to scan over the phrase array and the second over the word array. In semi-pseudocode, something like:

    NSMutableArray *filtered ... // etc.
    // Loop over each phrase.
    for (NSString *phrase in phrases) {
    
        // Let's assume it's acceptable
        bool good = true;
    
        for (NSString *word in words) {
    
            // If we find a single unwanted word, we'll no longer take it
            if ([phrase rangeOfString:word].location != NSNotFound) {
                good = false;
    
                break; // We don't need to keep iterating. 
                       // We already know it's not aceptable.
            }
        }
    
        if (good) [filtered insertObject:phrase];
    
    }
    

提交回复
热议问题