用struts标签 iterator 取list中对象的属性,字符串都成了ओ ;的形式
于是研究了一下:
java代码转译:
package com.zhh.util;
/*
*author zhh
*/
public class HTMLDecoder {
public static String decode(String str){
String[] tmp = str.split(";&#|&#|;");
StringBuffer sb = new StringBuffer("");
for (int i=0; i<tmp.length; i++ ){
if (tmp[i].matches("\\d{5}")){
sb.append((char)Integer.parseInt(tmp[i]));
} else {
sb.append(tmp[i]);
}
}
return sb.toString();
}
public static void main(String[] args){
String msg = decode("&# 31859;&# 24066;&# 24055;&# 34903;&# 36947"); //去掉符号和数字间空格再转
System.out.println(msg);
}
}
JavaScript代码转:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title> New Document </title>
<meta name="Generator" content="EditPlus">
<meta name="Author" content="">
<meta name="Keywords" content="">
<meta name="Description" content="">
</head>
<script type="text/javascript">
/*
*js HTML Encode
*/
var htmlEncode=function(str) {//HTML des encode.
var res=[];
for(var i=0;i < str.length;i++)
res[i]=str.charCodeAt(i);
return "&#"+res.join(";&#")+";";
};
var htmlEncode2=function(s) {
var r = "", c;
for (var i = 0; i < s.length; i++) {
c = s.charCodeAt(i);
r += (c < 32 || c == 38 || c > 127) ? ("&#" + c + ";") : s.charAt(i);
}
return r;
};
//s.replace(/([\u4e00-\u9fa5]+)/g,function($,$1) {
// return htmlEncode($1);
//})
var htmlHexEncode=function(str) {//HTML hex encode.
var res=[];
for(var i=0;i < str.length;i++)
res[i]=str.charCodeAt(i).toString(16);
return "&#"+String.fromCharCode(0x78)+res.join(";&#"+String.fromCharCode(0x78))+";";//x ,防止ff下&#x 转义
};
var htmlDecode = function(str) {
return str.replace(/&#(x)?([^&]{1,5});?/g,function($,$1,$2) {
return String.fromCharCode(parseInt($2 , $1 ? 16:10));
});
};
var s="htmlEncode HTML 编码转换 &#";
var s1=htmlEncode(s)+"\n\n只对双字节和&编码:"+htmlEncode2(s);
var s2=htmlDecode(s1);
alert("编码前:"+s+"\n\n编码后:"+s1+"\n\n解码后:"+s2);
var s="htmlHexEncode HTML 编码转换&#";
var s1=htmlHexEncode(s);
var s2=htmlDecode(s1);
alert("编码前:"+s+"\n\n编码后:"+s1+"\n\n解码后:"+s2);
var msg = "米市巷街道";
alert(msg);
var result = htmlDecode(msg);
alert(result);
</script>
<body>
</body>
</html>