escape()与flash.utils.escapeMultiByte()
escape()位于toplevel包中,它接收一个String类型的参数,将参数转换成字符串并编码成URL格式——其中大部分的非字母数字的字符会被%+十六进制序列替代。除数字和字母外,下面这些字符不会被编码:@ - _ . * + /。
ActionScript Code:
- var testStr:String = "hello 中国";
- trace("escape (", testStr, ") =", escape(testStr));
输出结果是
Output Code:
- escape ( hello 中国 ) = hello%20%u4E2D%u56FD
escapeMultiByte()是对接收的参数按照utf-8或则系统编码进行编码。如果Sytem.useCodePage为true,就按照系统码进行编码,否者按照utf-8编码。
ActionScript Code:
- import flash.system.System;
- import flash.utils.escapeMultiByte;
- System.useCodePage = false;
- var testStr:String = "hello 中国";
- trace("escapeMultiByte (", testStr, ") =", escapeMultiByte(testStr));
输出结果是
Output Code:
- escapeMultiByte ( hello 中国 ) = hello%20%E4%B8%AD%E5%9B%BD
ActionScript Code:
- import flash.system.System;
- import flash.utils.escapeMultiByte;
- System.useCodePage = true;
- var testStr:String = "hello 中国";
- trace("escapeMultiByte (", testStr, ") =", escapeMultiByte(testStr));
输出结果是
Output Code:
- escapeMultiByte ( hello 中国 ) = hello%20%D6%D0%B9%FA
以上我们可以看到在System.useCodePage不同的情况下使用escapeMultiByte编码后的结果不同,但是都和escape编码的结果不一样,那么escape又是按照什么编码的呢?”中”被编码成了%u4E2D,看格式是Unicode编码。在JScript中escape是采用ISO Latin字符集对指定的字符串进行编码,不知道Flash中是不是这样。
打开Adobe ExtendScript ToolKit2,输入如下代码
JavaScript Code:
- var testStr = "hello 中国";
- alert(escape(testStr));
运行后结果也是
Output Code:
- hello%20%u4E2D%u56FD

Leave a Reply