Remove HTML Tags from an NSString on the iPhone

前端 未结 22 1182
心在旅途
心在旅途 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条回答
  •  灰色年华
    2020-11-22 10:38

    A quick and "dirty" (removes everything between < and >) solution, works with iOS >= 3.2:

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

    I have this declared as a category os NSString.

提交回复
热议问题