I Love China

技术·人生

技术服务生活
繁體
5月 27th, 2007

escape()与flash.utils.escapeMultiByte()

escape()位于toplevel包中,它接收一个String类型的参数,将参数转换成字符串并编码成URL格式——其中大部分的非字母数字的字符会被%+十六进制序列替代。除数字和字母外,下面这些字符不会被编码:@ - _ . * + /

ActionScript Code:
  1. var testStr:String = "hello 中国";
  2. trace("escape (", testStr, ") =", escape(testStr));

输出结果是

Output Code:
  1. escape ( hello 中国 ) = hello%20%u4E2D%u56FD

escapeMultiByte()是对接收的参数按照utf-8或则系统编码进行编码。如果Sytem.useCodePage为true,就按照系统码进行编码,否者按照utf-8编码。

ActionScript Code:
  1. import flash.system.System;
  2. import flash.utils.escapeMultiByte;
  3. System.useCodePage = false;
  4. var testStr:String = "hello 中国";
  5. trace("escapeMultiByte (", testStr, ") =", escapeMultiByte(testStr));

输出结果是

Output Code:
  1. escapeMultiByte ( hello 中国 ) = hello%20%E4%B8%AD%E5%9B%BD
ActionScript Code:
  1. import flash.system.System;
  2. import flash.utils.escapeMultiByte;
  3. System.useCodePage = true;
  4. var testStr:String = "hello 中国";
  5. trace("escapeMultiByte (", testStr, ") =", escapeMultiByte(testStr));

输出结果是

Output Code:
  1. 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:
  1. var testStr = "hello 中国";
  2. alert(escape(testStr));

运行后结果也是

Output Code:
  1. hello%20%u4E2D%u56FD

随机文章:

Leave a Reply