代码之家  ›  专栏  ›  技术社区  ›  Lee Armstrong

iPhone字符串操作

  •  0
  • Lee Armstrong  · 技术社区  · 16 年前

    我有下面的绳子……

    Overall: 21 (1,192,742<img src="/images/image/move_up.gif" title="+7195865" alt="Up" />)<br />
    August: 21 (1,192,742<img src="/images/image/move_up.gif" title="+722865" alt="Up" />)<br />
    

    我需要删除HTML标记,是否可以说删除之间的所有内容????

    2 回复  |  直到 16 年前
        1
  •  1
  •   Paul McCabe    16 年前

    是否要从字符串中删除所有HTML内容?如果是这样的话,您可以通过以下方式接近它:

    - (void)removeHtml:(NSString *) yourString
    {
        NSString *identifiedHtml = nil;
    
        //Create a new scanner object using your string to parse
        NSScanner *scanner = [NSScanner scannerWithString: yourString];
    
        while (NO == [scanner isAtEnd])
        {
    
            // find opening html tag
            [scanner scanUpToString: @"<" intoString:NULL] ; 
    
            // find closing html tag - store html tag in identifiedHtml variable
            [scanner scanUpToString: @">" intoString: &identifiedHtml] ;
    
            // use identifiedHtml variable to search and replace with a space
            NSString yourString = 
                     [yourString stringByReplacingOccurrencesOfString:
                                 [ NSString stringWithFormat: @"%@>", identifiedHtml]
                                 withString: @" "];
    
        }
        //Log your html-less string
        NSLog(@"%@", yourString);
    }
    
        2
  •  0
  •   cocoafan    16 年前

    我不确定这是否适用于iPhone(因为initwithhtml:documentattributes:is an appkit addition),但我已经对其进行了Cocoa应用测试。

    NSString *text = "your posted html string here";        
    NSData *data = [text dataUsingEncoding: NSUnicodeStringEncoding];
    NSAttributedString *str = 
       [[[NSAttributedString alloc] initWithHTML: data documentAttributes: nil] autorelease];
    NSString *strippedString = [str string];