2015-01-07 66 views
-1

我需要解析html字符串。我有這樣的文字從字符串中刪除' t'和' n'

<html> <head>  </head> <body> <p style="margin-top: 0">  blbibibluboiubiubiu ibiub </p> </body></html> 

我已經刪除了'\ n'個字符。現在我需要刪除'\ t'字符。我試圖這樣做

String s = editor.getText(); 
s = s.replaceAll("\\n", ""); 
s = s.replaceAll("\\t", ""); 

但它不起作用。請幫忙

+0

您雙重轉義了您的轉義序列。用'「[\ t \ n]」'使用單個呼叫。投票結束爲錯字。 – dasblinkenlight

+0

這應該起作用,是什麼讓你覺得它沒有?但是,請注意,您不需要使用'replaceAll'(它接受正則表達式),使用'.replace(「\ n」,「」)和'.replace(「\ t 「,」「)'。 –

+1

你是否確定空格是製表符而不是空格? – Jens

回答

0

我建議你看看Jsoup或類似的框架,如果你要解析HTML。 Jsoup可以安全地刪除換行符和製表符。

實施例:

String html = "<html><head><title>First parse</title></head>" 
    + "<body><p>Parsed HTML into a doc.</p></body></html>"; 

// Now you can use the document to read your elements 
Document doc = Jsoup.parse(html); 

Jsoup Cookbook示出的示例的大量。

您也可以使用Jsoup到sanitize您的數據,即刪除不需要的屬性和標籤。

Jsoup.clean("<p>Some text</p>", Whitelist.none()); // -> "Some text" 

易於安裝,推薦!