替换:
[mutablestring replaceOccurrencesOfString:@"<" withString:@"<" options:0 range:NSMakeRange(0,[mutablestring length])];
[mutablestring replaceOccurrencesOfString:@">" withString:@">" options:0 range:NSMakeRange(0,[mutablestring length])];
正则表达式替换:
NSRegularExpression *regular;
regular = [[NSRegularExpression alloc] initWithPattern:@"</p>"
options:NSRegularExpressionCaseInsensitive
error:nil];
nomutablestring = [regular stringByReplacingMatchesInString:nomutablestring options:0 range:NSMakeRange(0, [nomutablestring length]) withTemplate:@"&&&"];
[regular release];
regular = [[NSRegularExpression alloc] initWithPattern:@"<p[^>]*TEXT-ALIGN: center[^>]*>"
options:NSRegularExpressionCaseInsensitive
error:nil];
nomutablestring = [regular stringByReplacingMatchesInString:nomutablestring options:0 range:NSMakeRange(0, [nomutablestring length]) withTemplate:@"<p>@center@"];
[regular release];
// 替换所有<>标签
regular = [[NSRegularExpression alloc] initWithPattern:@"<([^>]*)>"
options:NSRegularExpressionCaseInsensitive
error:nil];
nomutablestring = [regular stringByReplacingMatchesInString:nomutablestring options:0 range:NSMakeRange(0, [nomutablestring length]) withTemplate:@""];
[regular release];
去除字符串中所有得空格及控制字符:
responseString = [responseString stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet ]];
本文详细介绍了如何使用正则表达式进行字符串中的特定字符或模式替换,包括HTML标签替换、去除空格及控制字符等操作,提供了解决实际问题的实用技巧。
714

被折叠的 条评论
为什么被折叠?



