如何在 JavaScript 中編碼字串?


在這篇文章中,我們將學習如何使用合適的示例在 JavaScript 中編碼字串。

編碼是從一種資料格式轉換為另一種資料格式的過程。在計算機科學術語中,編碼是將文字轉換為密文的過程。編碼與加密過程不同。

編碼和加密的區別在於,編碼用於保持資料可用性,而加密用於維護資料機密性。編碼不使用金鑰進行編碼過程,而加密需要金鑰進行加密過程。在 JavaScript 中用於編碼字串的方法有 btoa()、encodeURI() 和 encodeURIComponent()。

使用 btoa() 方法

使用 btoa() 函式,將字串以 base-64 編碼轉換為 Base64 編碼的 ASCII 字串。btoa() 方法的語法如下:

btoa(string);

其中,引數 **string** 是要編碼的字串。返回值是編碼後的字串。

示例 1

這是一個使用 **btoa()** 方法編碼字串的示例程式。

<!DOCTYPE html>
<html lang="en">
<head>
   <meta charset="UTF-8">
   <meta http-equiv="X-UA-Compatible" content="IE=edge">
   <meta name="viewport" content="width=device-width, initial-scale=1.0">
   <title> Encoding a string in JavaScript.</title>
</head>
<body>
   <p id="encode"></p>
   <script>
      var str = "Tutorials point";
      document.getElementById('encode').innerHTML = 'The encoded string for "Tutorials point" is '+window.btoa(str);
   </script>
</body>
</html>

上述示例程式的輸出為:

使用 encodeURI() 方法

**encodeURI()** 函式透過將某些字元(如空格)替換為表示字元 UTF-8 編碼的轉義序列來編碼 URI 或字串。**encodeURI()** 方法的語法如下:

encodeURI(uri);

其中,引數 **uri** 是完整的 URI 或字串。返回值是編碼後的字串。

示例 2

這是一個使用 **encodeURI()** 方法編碼字串的示例程式。

<!DOCTYPE html>
<html lang="en">
<head>
   <meta charset="UTF-8">
   <meta http-equiv="X-UA-Compatible" content="IE=edge">
   <meta name="viewport" content="width=device-width, initial-scale=1.0">
   <title> Encoding a string in JavaScript.</title>
</head>
<body>
   <p id="encode"></p>
   <script>
      var str1 = "Tutorials point";
      var uri = "https://twitter.com/Google?ref_src=twsrc%5Egoogle";
      document.getElementById('encode').innerHTML = 'The encoded string for "Tutorials point" is '+encodeURI(str1)+'<br/>'+'The encoded string for "https://twitter.com/Google?ref_src=twsrc%5Egoogle" is '+encodeURI(uri);
   </script>
</body>
</html>

上述示例程式的輸出為:

使用 encodeURIComponent() 方法

**encodeURIComponent()** 方法是 encodeURI() 方法的增強版本。**encodeURI()** 和 **encodeURIComponent()** 方法的區別在於:使用 **encodeURI()** 編碼整個 URL,而使用 **encodeURIComponent()** 編碼 URI 元件(如查詢字串)。**encodeURIComponent()** 可以編碼 (‘#’,’&’,’$’,’/’,’:’,’;’,’@’,’?’) 等 **encodeURI()** 方法無法編碼的字元。**encodeURIComponent()** 方法的語法如下:

encodeURIComponent(value);

其中,**value** 可以是布林值、字串,數字會在編碼前轉換為字串。返回值是編碼後的字串。

示例 3

這是一個使用 **encodeURIComponent()** 方法編碼字串的示例程式。

<!DOCTYPE html>
<html lang="en">
<head>
   <meta charset="UTF-8">
   <meta http-equiv="X-UA-Compatible" content="IE=edge">
   <meta name="viewport" content="width=device-width, initial-scale=1.0">
   <title> Encoding a string in JavaScript.</title>
</head>
<body>
   <p id="encode"></p>
   <script>
      var str1 = "Tutorials point";
      var uriComp = "https://twitter.com/Google?ref_src=twsrc%5Egoogle";
      document.getElementById('encode').innerHTML = 'The encoded string for "Tutorials point" is '+encodeURIComponent(str1)+'<br/>'+'The encoded string for "https://twitter.com/Google?ref_src=twsrc%5Egoogle" is '+encodeURIComponent(uriComp);
   </script>
</body>
</html>

上述示例程式的輸出為:

更新於:2022年12月9日

24K+ 次瀏覽

開啟你的 職業生涯

完成課程獲得認證

開始學習
廣告
© . All rights reserved.