iphone开发使用得替换方法及正则表达式替换

来源:互联网 发布:影楼修片用什么软件 编辑:程序博客网 时间:2024/06/06 13:10

    替换:

    [mutablestring replaceOccurrencesOfString:@"&lt;" withString:@"<" options:0 range:NSMakeRange(0,[mutablestring length])];

    [mutablestring replaceOccurrencesOfString:@"&gt;" 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 ]];


原创粉丝点击