我有一个聊天室,使用名为blackList(一组不恰当的单词)的数组完美地过滤不适当的内容,并通过它迭代并用****替换不合适的单词。然而,当它遇到像“优雅”这样的词时,它会排出第三到第五个字母。如何阻止此问题及相关问题?谢谢,这是我的代码:
- (void)displayChatMessage:(NSString*)message fromUser:(NSString*)userName {
for(int i =0 ;i< blackList.count;i++)
{
NSMutableString* stars = [[NSMutableString alloc]init];
for(int j = 0 ; j <[[blackList objectAtIndex:i] length];j++)
[stars appendString:@"*"];
message = [message stringByReplacingOccurrencesOfString:(NSString*)[blackList objectAtIndex:i] withString:@"***"];
}
[chat appendTextAfterLinebreak:[NSString stringWithFormat:@"%@: %@", userName, message]];
[chat scrollToBottom:chat];
}