Replacing bad words in a string in Objective-C

I have a game with a public highscore where I allow layers to enter their name (or something up to 12 characters). I am trying to create a couple of functions for filtering bad words from a list of bad words

I have a text file. I have two methods:

One to read in a text file:

-(void) getTheBadWordsAndSaveForLater {

    badWordsFilePath = [[NSBundle mainBundle] pathForResource:@"badwords" ofType:@"txt"];
    badWordFile = [[NSString alloc] initWithContentsOfFile:badWordsFilePath encoding:NSUTF8StringEncoding error:nil];

    badwords =[[NSArray alloc] initWithContentsOfFile:badWordFile];
    badwords = [badWordFile componentsSeparatedByString:@"\n"];


    NSLog(@"Number Of Words Found in file: %i",[badwords count]);

    for (NSString* words in badwords) {

        NSLog(@"Word in Array----- %@",words);
    }


}

And one, to check the word (NSString*), is again on the list in which I read:

-(NSString *) removeBadWords :(NSString *) string {


    // If I hard code this line below, it works....
    // *****************************************************************************
    //badwords =[[NSMutableArray alloc] initWithObjects:@"shet",@"shat",@"shut",nil];
    // *****************************************************************************


    NSLog(@"checking: %@",string);

    for (NSString* words in badwords) {

       string = [string stringByReplacingOccurrencesOfString:words withString:@"-" options:NSCaseInsensitiveSearch range:NSMakeRange(0, string.length)];

        NSLog(@"Word in Array: %@",words);
    }

     NSLog(@"Cleaned Word Returned: %@",string);
    return string;
}

The problem I am facing is that when I hardcode the words into an array (see above), it works like a charm. But when I use the array that I read in the first method, it does not work - stringByReplacingOccurrencesOfString:wordsit seems to have no effect. I traced back to the magazine so that I could see if the words go, and they ... This one line does not seem to see the words, unless I'm hardcore in an array.

?

+4
1

:

  • :

    badwords =[[NSArray alloc] initWithContentsOfFile:badWordFile];
    badwords = [badWordFile componentsSeparatedByString:@"\n"];
    

    initWithContentsOfFile, componentsSeparatedByString . , initWithContentsOfFile , (plist), , . plist ( ), , . , .

    , , , initWithContentsOfFile, . :

    - (void)getTheBadWordsAndSaveForLater {
    
        // these should be local variables, so get rid of your instance variables of the same name
    
        NSString *badWordsFilePath = [[NSBundle mainBundle] pathForResource:@"badwords" ofType:@"txt"];
        NSString *badWordFile = [[NSString alloc] initWithContentsOfFile:badWordsFilePath encoding:NSUTF8StringEncoding error:nil];
    
        // calculate `badwords` solely from `componentsSeparatedByString`, not `initWithContentsOfFile`
    
        badwords = [badWordFile componentsSeparatedByString:@"\n"];
    
        // confirm what we got
    
        NSLog(@"Found %i words: %@", [badwords count], badwords);
    }
    
  • , , :

    - (NSString *) removeBadWords:(NSString *) string {
    
        NSLog(@"checking: %@ for occurrences of these bad words: %@", string, badwords);
    
        for (NSString* badword in badwords) {
            NSString *searchString = [NSString stringWithFormat:@"\\b%@\\b", badword];
            string = [string stringByReplacingOccurrencesOfString:searchString
                                                       withString:@"-"
                                                          options:NSCaseInsensitiveSearch | NSRegularExpressionSearch
                                                            range:NSMakeRange(0, string.length)];
        }
    
        NSLog(@"resulted in: %@", string);
    
        return string;
    }
    

    " ", \b " ". , \bhell\b (, NSString, @"\\bhell\\b") "", , "", .

  • , , badwords, , reset - . , , , , . , badwords , .

+4

All Articles