2009-08-15 45 views
0

我有以下字符串....iphone字符串操作

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標籤,有沒有辦法,我可以說之間移除一切???

+0

什麼是你想要結束的確切字符串?你想從整個文本中刪除尖括號內的所有內容嗎? – 2009-08-15 21:30:02

+0

你是否設法解決這個問題? – 2009-08-28 13:08:53

回答

1

你希望刪除所有的你的字符串中的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); 
} 
0

我不知道這是否會在iPhone上運行(因爲initWithHTML:documentAttributes:是AppKit的加法),但我測試過它的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]; 
+0

http://stackoverflow.com/questions/729135/why-no-nsattributedstring-on-the-iphone似乎表明你不能在iPhone上使用它 – 2009-08-16 03:48:33