java和javascript(js)实现url转码encode和decode
关键词 URLEncoder URLDecoder escape encodeURI encodeURIComponent decodeURI unescape
java
String s=URLEncoder.encode("这是上岛咖啡你哈卡斯","utf-8");
String s2= URLDecoder.decode("%E8%BF%99%E6%98%AF%E4%B8%8A%E5%B2%9B%E5%92%96%E5%95%A1%E4%BD%A0%E5%93%88%E5%8D%A1%E6%96%AFjavascript%3Avoid(0)%3B","utf-8");
System.out.println(s);
System.out.println(s2);
js
var str = '这是什么'
console.log(str)
console.log(escape(str), '使用escape()编码')
console.log(encodeURI(str), '使用encodeURI()编码')
console.log(encodeURIComponent(str), '使用encodeURIComponent()编码')
var str1 = '%E4%BB%80%E4%B9%88'
console.log(unescape(str1), '使用escape()编码后,使用unescape()解码')
var str2 = '%E4%BB%80%E4%B9%88'
console.log(decodeURI(str2), '使用encodeURI()编码后,使用decodeURI()解码')
var str3 = '%E4%BB%80%E4%B9%88'
console.log(decodeURIComponent(str3), '使用encodeURIComponent()编码后,使用decodeURIComponent()解码')
1.escape()
该方法不会对 ASCII 字母和数字进行编码,也不会对下面这些 ASCII 标点符号进行编码: - _ . ! ~ * ' ( ) 。其他所有的字符都会被转义序列替换。
2.encodeURI()
该方法的目的是对 URI 进行完整的编码,因此对以下在 URI 中具有特殊含义的 ASCII 标点符号,encodeURI() 函数是不会进行转义的:;/?:@&=+$,#
3.encodeURIComponent()
该方法不会对 ASCII 字母和数字进行编码,也不会对这些 ASCII 标点符号进行编码: - _ . ! ~ * ' ( ) 。
fixed
没有一个冬天不可逾越,没有一个春天不会来临。最慢的步伐不是跬步,而是徘徊,最快的脚步不是冲刺,而是坚持。