1
我已经扩展了用It
设置的黑名单词,以发现一些单元测试现在失败。如何分解一个字符串并排除一些单词?
我没有注意到I
现在也被排除在外,因为它匹配It
,这不是我的意图。
notes = @"I was out with Jenny for dinner. It was raining all night.";
NSString * const BLACKLISTEDWORDS = @"in,it,It";
NSArray *words = [notes componentsSeparatedByString:@" "];
for (NSString *word in words) {
if([BLACKLISTEDWORDS rangeOfString:word].location == NSNotFound]) {
}
}
有没有更好的方法来创建黑名单方法?
解决方案: 马特解决方案工作正常。捕捉到它的代码,此解决方案:
NSSet *blackList = [NSSet setWithArray:[BLACKLISTEDWORDS componentsSeparatedByString:@","]];
for (NSString *word in words) {
if (![blackList containsObject:word]) {
}
}