[英]Convert a string with Unicode characters in decimal format(html encoded) to a regular string
我有一個Map類型的變量。
if (sourceMap.containsKey(currentRow)) {
//Remove the row from Map
} else {
//Mismatch
}
其中sourceMap
是一個Hashmap變量,其中包含許多字符串,例如
Period Name
Person Last Name
Person First Name
Order Code
Ship_to_Customer_Name
Sub_Profit_Center
Commission Amount
Credit Amount
Rate Amount
Apr-09
Morgan
Martin
1022334852
Carl Zeiss de M&# 195;&# 189;xico, S.A. de C.V.(no space after the # in the string)
並且currentRow
包含以下字符串:
Carl Zeiss de Mýxico, S.A. de C.V.
與最后一行相同。我的要求是應匹配。 現在不匹配了,我該怎么做才能匹配那些
這些字符串來自不同的文件,第一個以CSV格式下載,因此沒有Unicode字符。.第二個(currentRow)以未編碼的txt格式下載,並使用dos2unix轉換為CSV。
Carl Zeiss de Mýxico, S.A. de C.V.
這是一個帶有HTML編碼字符的字符串。 您可以使用諸如unescapeHtml4
類的實用程序函數來進行HTML- unescapeHtml4
。
通常,您希望將字符串保留為原始格式,而不是使用HTML轉義符。 看看無論您在哪里都可以找到sourceMap
如果您控制了它並可以解決它,避免了不必要的轉義,那么這將是sourceMap
。 還要注意的是,無論是否經過HTML編碼, Mýxico
看起來像是在堆棧中其他地方誤處理了Unicode字符的證據。
首先,我們必須從以下URL下載jar文件http://www.java2s.com/Code/Jar/c/Downloadcommonlang3jar.htm
現在添加import語句,如下所示
import static org.apache.commons.lang3.StringEscapeUtils.unescapeHtml4;
現在,我們需要在要轉義html編碼的字符串的地方使用該方法。 例如:
String s=Carl Zeiss de Mýxico, S.A. de C.V.
System.out.println("Before: "+s);
s=unescapeHtml4(s);
System.out.println("After: "+s);
現在輸出如下
Before: Carl Zeiss de Mýxico, S.A. de C.V.
After: Carl Zeiss de Mýxico, S.A. de C.V.
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.