How to remove html tags and trim the characters to

2019-08-16 16:34发布

I am displaying html data in webView (lot of content).Now i want to remove all the tags and trim the content to only 250 charecters and display in my Web-view.

Thanks In Advance....

1条回答
一纸荒年 Trace。
2楼-- · 2019-08-16 16:55

Included this function in the class.

in .h

- (NSString *)stringByStrippingHTML:(NSString *)inputString;

in .m

- (NSString *)stringByStrippingHTML:(NSString *)inputString 
{
  NSMutableString *outString;

  if (inputString)
  {
    outString = [[NSMutableString alloc] initWithString:inputString];

    if ([inputString length] > 0)
    {
      NSRange r;

      while ((r = [outString rangeOfString:@"<[^>]+>" options:NSRegularExpressionSearch]).location != NSNotFound)
      {
        [outString deleteCharactersInRange:r];
      }      
    }
  }

  return outString; 
}

the call

  NSString *plainString = [self stringByStrippingHTML:inputHTMLString ];


    NSString *rangedString = [plainString substringToIndex:249];  //0 to 249 makes it 250 characters
查看更多
登录 后发表回答