Remove HTML Tags from an NSString on the iPhone

前端 未结 22 1614
心在旅途
心在旅途 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:18

    Another one way:

    Interface:

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

    Implementation

    (NSString *) stringByStrippingHTML:(NSString*)inputString
    { 
    NSAttributedString *attrString = [[NSAttributedString alloc] initWithData:[inputString dataUsingEncoding:NSUTF8StringEncoding] options:@{NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType,NSCharacterEncodingDocumentAttribute: @(NSUTF8StringEncoding)} documentAttributes:nil error:nil];
    NSString *str= [attrString string]; 
    
    //you can add here replacements as your needs:
        [str stringByReplacingOccurrencesOfString:@"[" withString:@""];
        [str stringByReplacingOccurrencesOfString:@"]" withString:@""];
        [str stringByReplacingOccurrencesOfString:@"\n" withString:@""];
    
        return str;
    }
    

    Realization

    cell.exampleClass.text = [self stringByStrippingHTML:[exampleJSONParsingArray valueForKey: @"key"]];

    or simple

    NSString *myClearStr = [self stringByStrippingHTML:rudeStr];

提交回复
热议问题