![](/img/trans.png)
[英]Java: Read a txt file and save it in an array of strings but without backslashes (spaces)?
[英]Convert html strings with backslashes in JAVA
如何將以下帶有反斜杠的字符串轉換為下面的字符串? JAVA中是否有這樣做的功能,或者我必須使用replace?
'<div class=\”divcontent\"><p style=\"padding:0px;\"><p><strong>title<\/strong><\/p><\/div>’
<div class=”divcontent"><p style="padding:0px;"><p><strong>title</strong></p></div>’
您可以使用Apache Commons Lang的StringEscapeUtils方法。
import org.apache.commons.lang3.StringEscapeUtils;
// ...
String html = StringEscapeUtils.unescapeJava("<div class=\"divcontent\"><p style=\"padding:0px;\"><p><strong>title<\/strong><\/p><\/div>");
為什么我們不使用unescapeHtml()方法? 例如,因為它嘗試轉換<
到<
和>
到>
。 但是我們對他們沒有任何問題。 我們很容易刪除標准的Java轉義序列。
是的,請使用replaceAll
方法:
Java字符串Replace replaceAll
'
與' replace
'方法不同,' replaceAll
'支持正則表達式。 ' replaceAll
'的功能使在字符串對象中進行子字符串的復雜搜索成為可能。 搜索后,將每個匹配的子字符串替換為給定的替換。 以下是' replaceAll
'方法的格式:
String replaceAll (String regEx, String replacement)
String sampleString = new String ("Remove All backslashes");
String resultantString = sampleString.replaceAll ("\\","");
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.