How to remove html tags and trim the characters to 250 in Objective C

核能气质少年 提交于 2019-12-13 21:35:43

问题


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:


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


来源:https://stackoverflow.com/questions/12894955/how-to-remove-html-tags-and-trim-the-characters-to-250-in-objective-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!