Remove HTML Tags from an NSString on the iPhone

前端 未结 22 1176
心在旅途
心在旅途 2020-11-22 10:02

There are a couple of different ways to remove HTML tags from an NSString in Cocoa.

One way is to render the string into an

22条回答
  •  -上瘾入骨i
    2020-11-22 10:39

    This is the modernization of m.kocikowski answer which removes whitespaces:

    @implementation NSString (StripXMLTags)
    
    - (NSString *)stripXMLTags
    {
        NSRange r;
        NSString *s = [self copy];
        while ((r = [s rangeOfString:@"<[^>]+>\\s*" options:NSRegularExpressionSearch]).location != NSNotFound)
            s = [s stringByReplacingCharactersInRange:r withString:@""];
        return s;
    }
    
    @end
    

提交回复
热议问题