嗨,我有一个新闻阅读器,我不断收到这样的字符串 -
two New York police officers shot dead in 'ambush'
I want the string two New York police officers shot dead in ambush
我如何从&至 ;然后删除扫描的出现次数。
我创建了一个像这样的扫描仪 -
NSString *webString222222 = filteredTitle2;
NSScanner *stringScanner222222 = [NSScanner scannerWithString:webString222222];
NSString *content222222 = [[NSString alloc] init];
[stringScanner222222 scanUpToString:@"&" intoString:Nil];
[stringScanner222222 scanUpToString:@";" intoString:&content222222];
NSString *filteredTitle222 = [content222222 stringByReplacingOccurrencesOfString:content222222 withString:@""];
NSString *filteredTitle22 = [filteredTitle222 stringByReplacingOccurrencesOfString:@"&" withString:@""];
但是当我执行此代码时,整个文本消失了!每一个字。 当我检查我的NSLog中的标题是唯一的&签到那里和唯一的;在那里签到! 我不知道我在哪里出错了。
答案 0 :(得分:1)
如果您遇到的特殊字符都相对一致,您只需用空字符串替换每个子字符串,如下所示:
NSString *cleansedString = [filteredTitle2 stringByReplacingOccurrencesOfString:@"'"
withString:@""];
答案 1 :(得分:0)
您可以使用NSRegularExpression
构建正确的匹配模式:
NSString *pattern = @"\\&#[0-9]+;";
NSString *str = @"two New York police officers shot dead in 'ambush'";
NSLog(@"Original test: %@",str);
NSError *error = nil;
NSRegularExpression *regex = [NSRegularExpression
regularExpressionWithPattern:pattern
options:NSRegularExpressionCaseInsensitive error:&error];
if (error != nil)
{
NSLog(@"ERror: %@",error);
}
else
{
NSString *replaced = [regex stringByReplacingMatchesInString:str
options:0
range:NSMakeRange(0, [str length])
withTemplate:@""];
NSLog(@"Replaced test: %@",replaced);
}