2013-03-11 101 views
1

我是一個noob iphone開發,我試圖將html數據轉換爲字符串格式。目前,我正在使用stringByConvertingHTMLToPlainText來完成此操作,但它忽略了HTML的格式。我要轉換的HTML數據有換行符,但stringByConvertingHTMLToPlainText只刪除HtML標籤並且不格式化結果字符串。任何幫助是極大的讚賞。如何正確地將HTML轉換爲字符串?

我的代碼:

NSString *temp = [[[stories objectAtIndex: storyIndex] objectForKey: @"summary"]stringByConvertingHTMLToPlainText]; 

HTML數據:

Holiday Inn<br/>Dedham,MA<br/>Sun May 5th 2013-Sun May 5th 2013<br/>Contact: Harry Tong at 603-978-3459<p><sub><i>-- Delivered by <a href="http://feed43.com/">Feed43</a> service</i></sub></p> 

得到的字符串:

Holiday InnDedham,MASun May 5th 2013-Sun May 5th 2013Contact: Harry Tong at 603-978-3459-- Delivered by "http://feed43.com/"Feed43 service 

我需要:

Holiday Inn 
Dedham,MA 
Sun May 5th 2013-Sun May 5th 2013 
Contact: Harry Tong at 603-978-3459 
-- Delivered by "http://feed43.com/"Feed43 service 

編輯

NSString *html = [[stories objectAtIndex: storyIndex] objectForKey: @"summary"]; 
NSString *text = [html stringByReplacingOccurrencesOfString:@"<br/>" withString:@" \n "]; 
NSString *temp = [text stringByConvertingHTMLToPlainText]; 

回答

1

我想你應該只是換行符替換< BR/>(也許<BR>老年HTML的情況下)。在您正在編程的語言中Google換行符,並使用字符串替換函數。

+0

感謝您的回覆。你的回答很有道理,但由於某種原因,它對我不起作用。請檢查我的編輯 – 2013-03-11 22:35:01

+0

在*文本中跳過第三步(* temp = ...)您已經擁有了您想要的內容 – Prozi 2013-03-11 23:01:17

1

@Prozi是對的。您可以使用

NSString *text = [html stringByReplacingOccurrencesOfString:@"<br/>" withString:@"\n"]; 

做那您刪除HTML標籤。

+0

感謝您的回覆。你的回答很有道理,但由於某種原因,它對我不起作用。請檢查我的編輯。 – 2013-03-11 22:31:50