在Objective-C中替换字符串中的坏单词

在Objective-C中替换字符串中的坏单词,objective-c,file,nsstring,Objective C,File,Nsstring,我有一个游戏,有一个公开的高分列表,我允许层输入他们的名字(或任何12个字符)。我正在尝试创建两个函数,从一个坏单词列表中过滤掉坏单词 我有一个文本文件。我有两种方法: 要在文本文件中读取的一个: -(void) getTheBadWordsAndSaveForLater { badWordsFilePath = [[NSBundle mainBundle] pathForResource:@"badwords" ofType:@"txt"]; badWordFile = [[

我有一个游戏,有一个公开的高分列表,我允许层输入他们的名字(或任何12个字符)。我正在尝试创建两个函数,从一个坏单词列表中过滤掉坏单词

我有一个文本文件。我有两种方法:

要在文本文件中读取的一个:

-(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);
    }


}
还有一个是在我读到的列表中再次检查单词
(NSString*)

-(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;
}
我遇到的问题是,当我将单词硬编码到一个数组中时(参见上面的注释),它就像一个符咒。但是当我使用第一个方法读入的数组时,它不起作用-stringByReplacingOccurrencesOfString:words似乎没有效果。我已经查到了日志,所以我可以看看这些词是否通过了,它们是。。。这一行似乎看不到单词,除非我硬插入数组

有什么建议吗?

一些想法:

  • 你有两行:

    badwords =[[NSArray alloc] initWithContentsOfFile:badWordFile];
    badwords = [badWordFile componentsSeparatedByString:@"\n"];
    
    如果您只是想用下一行中的字符串分隔的
    组件替换它,那么用contentsoffile
    初始化没有任何意义。另外,
    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”
    )将搜索单独的单词“hell”,但与“hello”不匹配

  • 注意,在上面,我还记录了
    badwords
    ,以查看该变量是否以某种方式重置。考虑到您所描述的症状,这是唯一有意义的事情,即从文本文件加载坏单词是可行的,但替换过程失败。因此,在更换之前,请检查
    不良词语
    ,并确保其设置正确


  • 你的代码没有多大意义。您可以从位于badWordsFilePath的文件中加载badWordFile,然后从位于badWordsFile的文件中加载badwords。然后使用通过componentsSeparatedByString处理的badWordsFile覆盖该值(无论它是什么)。但主要问题可能是无法从读取数组的元素中删除回车符(该数组可能是使用记事本或添加回车符的某些元素创建的)。用
    空格和换行字符集
    尝试
    stringByTrimmingCharactersInSet
    (在每个单词上)。顺便说一句,这个方案充满了危险。许多完全合法的词里面都含有“坏”字。甚至在不同的语境中,“月亮在新降雪的胸前,给下面的物体带来了正午的光泽”,即使是(稍微有点)不好的词也是完全可以的。谢谢你的建议-你能告诉我我对XCODE有点陌生吗?至于这个计划正面临危险,我同意并欢迎一个更好的建议——我知道,如果我不在其中加入任何保障措施,它将被滥用。HOTLICKS——它成功了!非常感谢-谢谢你的指导。很好!