iphone開發使用得替換方法及正則表示式替換
替換:
[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>@
[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 ]];