我正在尝试使用此代码过滤单词
-(BOOL)isBadWord:(NSString*)string{
NSString* path = [[NSBundle mainBundle] pathForResource:@"wordlist"
ofType:@"txt"];
NSString* content = [NSString stringWithContentsOfFile:path
encoding:NSUTF8StringEncoding
error:NULL];
NSString *delimiter = @"\n";
NSArray *items = [content componentsSeparatedByString:delimiter];
NSString *character = @" ";
NSArray *searchItems = [string componentsSeparatedByString:character];
BOOL isContain = false;
for (int i = 0; i < searchItems.count; i++) {
if (![[searchItems objectAtIndex:i] isEqual:@""]) {
NSUInteger indexOfTheObject =[items containsObject:[searchItems objectAtIndex:i]];
if (indexOfTheObject > 0) {
isContain = true;
}
}
}
return isContain;
}
这对于单个单词是可以的,但如果文本文件中的单词组合它不起作用。例如:字符串 = word1 {空格} word2