AS3 編碼轉換函式__亂碼問題

來源:互聯網
上載者:User

AS3 編碼轉換函式 Tags:  函數,  編碼

as3.0新增公用類函數,用法類似與javascript裡的escape、encodeURI、encodeURIComponent等...

1.decodeURI () 函數  [as3.0新增] 

as3.0文法: public function decodeURI(uri:String):String

文法: public function decodeURI(uri:String):String

返回一個已編碼的統一資源識別項 (URI) 的非編碼形式。
如果將編碼結果傳遞給 encodeURI,則將返回初始的字串。decodeURI 不對下列字元進行編碼:“:”、“/”、“;”和“?”。請使用 decodeURIComponent 對這些字元進行編碼。

輸入  輸出
%23        # 
%24        $ 
%26        & 
%2B        + 
%2C        , 
%2F        / 
%3A        : 
%3B        ; 
%3D        = 
%3F        ? 
%40        @ 

參數:

uri:String--表示編碼 URI 的字串。

返回:字串

執行個體: 複製內容到剪貼簿 代碼:package {
    import flash.display.Sprite;
    public class decodeURIExample extends Sprite {
        public function decodeURIExample() {
        var uri:String = "http://www.example.com/application.jsp?user=%3Cuser%20name='some%20user'%3E%3C/user%3E";
                   var decoded:String=decoded(uri);
        trace(decoded);    // http://www.example.com/application.jsp?user=&lt;user name='some user'></user>
          }
        }
    }
}
2.decodeURIComponent () 函數   [as3.0新增]

as3.0文法: public function decodeURIComponent(uri:String):String

返回統一資源識別項 (URI) 的一個已編碼組件的非編碼形式。

參數:

uri:String--一個表示已編碼的 URI 組件的值.

返回:字串

執行個體: 複製內容到剪貼簿 代碼:???
3.encodeURI () 函數 [as3.0新增]

as3.0文法: public function encodeURI(uri:String):String

返回編碼為有效統一資源識別項 (URI) 的字串。
如果將編碼結果傳遞給 decodeURI,則將返回初始的字串。encodeURI 不對下列字元進行編碼:“:”、“/”、“;”和“?”。請使用 encodeURIComponent 對這些字元進行編碼。

非編碼:

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 的字串

返回:字串 複製內容到剪貼簿 代碼:package {
    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 encode:String=encodeURI(uri);
        trace(encode);    // http://www.example.com/application.jsp?user=%3Cuser%20name='some%20user'%3E%3C/user%3E
          }
        }
    }
}
4.encodeURIComponent () 函數   [as3.0新增]

as3.0文法: public function encodeURIComponent(uri:String):String

返回編碼為統一資源識別項 (URI) 的有效組件的字串。

參數:
uri:String--表示編碼 URI 組件的字串。 

返回:字串

執行個體: 複製內容到剪貼簿 代碼:???
5.escape () 函數 

as3.0文法: public function escape(str:String):String

返回一個可在所有電腦上讀取的編碼 String 對象。
將參數轉換為字串,並以 URL 編碼格式對其進行編碼,在這種格式中,所有非字母數位字元都替換為 % 十六進位序列。當用於 URL 編碼的字串中時,百分比符號 (%) 用於引入轉義符,不與模運算子 (%) 等效。 
參數:
str:String--要編碼的任何 String 對象或文本(轉換為字串並以 URL 編碼格式進行編碼的表達)

返回:字串

執行個體一: 複製內容到剪貼簿 代碼://下面的代碼產生結果 someuser%40somedomain%2Ecom:
var email:String = "someuser@somedomain.com";
trace(escape(email));
執行個體二:
在此樣本中,at 符號 (@) 被替換為 %40,而點符號 (.) 被替換為 %2E。如果您要嘗試將資訊傳遞到遠程伺服器,並且資料中包含特殊字元(例如,& 或 ?),則它非常有用,如下面的代碼所示: 複製內容到剪貼簿 代碼:var redirectUrl:String = "http://www.somedomain.com?loggedin=true&username=Gus";
sendToURL("http://www.myothersite.com?returnurl="+ escape(redirectUrl));
6.unescape () 函數

as3.0文法: public function unescape(str:String):String


將參數 str:String作為字串計算,將該字串從 URL 編碼格式解碼(將所有十六進位序列轉換為 ASCII 字元),並返回該字串。

參數:
str:String--要轉義的十六進位序列字串

返回:字串

執行個體:

下面的樣本說明轉義到反向轉義的轉換過程: 複製內容到剪貼簿 代碼:var email:String = "user@somedomain.com";
trace(email);
var escapedEmail:String = escape(email);
trace(escapedEmail);
var unescapedEmail:String = unescape(escapedEmail);
trace(unescapedEmail);

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.