zoukankan      html  css  js  c++  java
  • encodeURI、encodeURIComponent、escape区别

      在as3中有三个可以对字符串编码的函数,分别是: escape,encodeURI,encodeURIComponent,相应3个解码函数:unescape,decodeURI,decodeURIComponent 。

        
        1 public function escape(str:String):String

         将参数转换为字符串,并以 URL 编码格式对其进行编码,在这种格式中,大多数非字母数字的字符都替换为 % 十六进制序列。当用于 URL 编码的字符串时,百分号 (%) 用于引入转义字符,不与模运算符 (%) 等效。

    下表显示不会 由 escape() 函数转换为转义序列的所有字符。

    未编码的字符

    0 1 2 3 4 5 6 7 8 9 
    a b c d e f g h i j k l m n o p q r s t u v w x y z 
    A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
     
    @ - _ . * + /



    参数  str:String — 要转换为字符串并以 URL 编码格式进行编码的表达式。 

    返回 String — 一个 URL 编码的字符串。 

         public function encodeURI(uri:String):String

            将字符串编码为有效的 URI(统一资源标识符)。将完整的 URI 转换为一个字符串,其中除属于一小组基本字符的字符外,其他所有字符都以 UTF-8 转义序列进行编码。

            下表显示不会 由 encodeURI 函数转换为 UTF-8 转义序列的基本字符的整个集合。

            未编码的字符       

    0 1 2 3 4 5 6 7 8 9 
    a b c d e f g h i j k l m n o p q r s t u v w x y z 
    A B C D E F G H I J K L M N O P Q R S T U V W X Y Z 
    ; / ? : @ & = + $ , # 
    - _ . ! ~ * ' ( )

            

    参数  uri:String — 一个表示完整 URI 的字符串。 

    返回 String — 一个字符串,其中某些字符已编码为 UTF-8 转义序列。 

    示例

        import flash.display.Sprite;
    
        public class EncodeURIExample extends Sprite {
            public function EncodeURIExample() {
                var uri:String = "http://www.example.com/application.jsp?user=<user name='some user'></user>";
                var encoded:String = encodeURI(uri);
                var decoded:String = decodeURI(encoded);
                trace(uri);        // http://www.example.com/application.jsp?user=<user name='some user'></user>
                trace(encoded);    // http://www.example.com/application.jsp?user=%3Cuser%20name='some%20user'%3E%3C/user%3E
                trace(decoded);    // http://www.example.com/application.jsp?user=<user name='some user'></user>
            }
        }
    

      

     3  public function encodeURIComponent(uri:String):String

            将字符串编码为有效的 URI 组件。将 URI 的子字符串转换为一个字符串,其中除属于非常小的一组基本字符的字符外,其他所有字符都以 UTF-8 转义序列进行编码。

            encodeURIComponent() 函数与 encodeURI() 函数不同,它仅适用于 URI 字符串的一部分(称为 URI 组件)。URI 组件是指出现在某些特殊字符之间的任何文本,

            这些特殊字符称为组件分隔符(: / ; 和 ? )。      “http”和“www.adobe.com”是常见的 URI 组件示例。

            此函数与 encodeURI() 的另一个重要区别是:由于此函数假定它处理的是 URI 组件,因此它会将特殊分隔符字符 (; / ? : @ & = + $ , #) 视为应进行编码的常规文本。

            下表显示不会 由 encodeURIComponent 函数转换为 UTF-8 转义序列的所有字符。

            未编码的字符 

    0 1 2 3 4 5 6 7 8 9 
    a b c d e f g h i j k l m n o p q r s t u v w x y z
    A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
    - _ . ! ~ * ' ( )
     



    参数  uri:String

    返回 String

       总结:1 如果要对一个URI全部进行编码,采用encodeURI函数。

                2 如果要对URI中的参数,特别是中文参数、特殊字符进行转移,采用encodeURIComponent函数。

                3 escape()除了 ASCII 字母、数字和特定的符号外,对传进来的字符串全部进行转义编码,因此如果想对URL编码,最好不要使用此方法。

                4 tomcat能解析 escape,但是websphere却不能解析,就会导致找不到url资源(例如:1.swf)。

    一个人成就的大小与承担责任的多少是成正比
  • 相关阅读:
    LightOJ 1132 Summing up Powers(矩阵快速幂)
    hdu 3804 Query on a tree (树链剖分+线段树)
    LightOJ 1052 String Growth && uva 12045 Fun with Strings (矩阵快速幂)
    uva 12304 2D Geometry 110 in 1! (Geometry)
    LA 3263 That Nice Euler Circuit (2D Geometry)
    2013 SCAUCPC Summary
    poj 3321 Apple Tree (Binary Index Tree)
    uva 11796 Dog Distance (几何+模拟)
    uva 11178 Morley's Theorem (2D Geometry)
    动手动脑
  • 原文地址:https://www.cnblogs.com/qxoffice2008/p/3960851.html
Copyright © 2011-2022 走看看