我正在使用StringEscapeUtils轉義和unescape html。我有以下代碼字符串內容相同,但等於方法返回false
import org.apache.commons.lang.StringEscapeUtils;
public class EscapeUtils {
public static void main(String args[]) {
String string = " 4-Spaces ,\"Double Quote\", 'Single Quote', \\Back-Slash\\, /Forward Slash/ ";
String escaped = StringEscapeUtils.escapeHtml(string);
String myEscaped = escapeHtml(string);
String unescaped = StringEscapeUtils.unescapeHtml(escaped);
String myUnescaped = StringEscapeUtils.unescapeHtml(myEscaped);
System.out.println("Real String: " + string);
System.out.println();
System.out.println("Escaped String: " + escaped);
System.out.println("My Escaped String: " + myEscaped);
System.out.println();
System.out.println("Unescaped String: " + unescaped);
System.out.println("My Unescaped String: " + myUnescaped);
System.out.println();
System.out.println("Comparison:");
System.out.println("Real String == Unescaped String: " + string.equals(unescaped));
System.out.println("Real String == My Unescaped String: " + string.equals(myUnescaped));
System.out.println("Unescaped String == My Unescaped String: " + unescaped.equals(myUnescaped));
}
public static String escapeHtml(String s) {
String escaped = "";
if(null != s) {
escaped = StringEscapeUtils.escapeHtml(s);
escaped = escaped.replaceAll(" "," ");
escaped = escaped.replaceAll("'","'");
escaped = escaped.replaceAll("\\\\","\");
escaped = escaped.replaceAll("/","/");
}
return escaped;
}
}
輸出:
Real String: 4-Spaces ,"Double Quote", 'Single Quote', \Back-Slash\, /Forward Slash/
Escaped String: 4-Spaces ,"Double Quote", 'Single Quote', \Back-Slash\, /Forward Slash/
My Escaped String: 4-Spaces ,"Double Quote", 'Single Quote', \Back-Slash\, /Forward Slash/
Unescaped String: 4-Spaces ,"Double Quote", 'Single Quote', \Back-Slash\, /Forward Slash/
My Unescaped String: 4-Spaces ,"Double Quote", 'Single Quote', \Back-Slash\, /Forward Slash/
Comparison:
Real String == Unescaped String: true
Real String == My Unescaped String: false
Unescaped String == My Unescaped String: false
我escaped
真正string
,然後unescaped
它。但myEsceped
首先用相同的進程轉義,然後用他們的html代碼替換一些更多的html字符。 myUnescaped
實際上是myEscaped
的內容,它與真實字符串的內容相同。
輸出顯示真實string
,unescaped
和myUnescaped
內容相同。但是,如比較部分所示,myUnescaped
不等於string
和unescaped
。
我不明白它在這裏實際發生了什麼。任何人都可以解釋嗎?
噢,我的頭在旋轉 – muneebShabbir 2013-04-25 06:44:50
可以請你調試和檢查字符串的字符數組,驗證和請分享 – muneebShabbir 2013-04-25 06:52:47
我不看行'轉義的字符串==我轉義的字符串:'在你的代碼。你可以在你的程序中添加這個比較的部分嗎? – Patashu 2013-04-25 07:04:04