JavaScript fromCodePoint() 方法



JavaScript String 的 fromCodePoint() 方法返回一個由指定程式碼點值序列建立的字串。您可以將一個或多個 Unicode 程式碼點值傳遞給此方法。如果傳遞的值不是整數,小於 0(或負數),或大於 0x10FFFF,它將丟擲 RangeError 異常。此方法是 靜態的,可以使用類語法呼叫,例如 String.fromCodePoint(),而不是在變數上呼叫它。

Unicode 程式碼點是一個數值,對應於 Unicode 標準中特定的字元。每個字元和符號都有其唯一的 Unicode 程式碼點值。

語法

以下是 JavaScript String fromCodePoint() 方法的語法:

String.fromCodePoint(num1, num2, /*..., */ numN)

引數

此方法接受多個相同型別的引數,例如 'num1'、'num2'、'num3' 直到 'numN',如下所述:

  • num1, num2....numN − 它是一個整數,表示範圍 [0, 0x10FFFF] 內的 Unicode 程式碼點。

返回值

此方法返回由指定程式碼點序列建立的字串。

示例 1

如果我們將 單個 Unicode 程式碼點值傳遞給此方法,它將返回僅包含一個字元的字串。

在以下示例中,我們使用 JavaScript String fromCodePoint() 方法檢索由指定的 Uni 程式碼點值 65 建立的字串。

<html>
<head>
<title>JavaScript String fromCodePoint() Method</title>
</head>
<body>
<script>
   const unicodepoint = 65;
   document.write("Uni code point: ", unicodepoint);
   document.write("<br>The uni code point ", unicodepoint, " represents ", String.fromCodePoint(unicodepoint));
</script>
</body>
</html>

輸出

以上程式返回字串“A”。

Uni code point: 65
The uni code point 65 represents A

示例 2

如果傳遞了多個 Unicode 值,則該方法將返回包含多個字元的字串。

以下是 JavaScript String fromCodePoint() 方法的另一個示例。我們使用此方法檢索包含由指定的 uni 程式碼序列 84、117、116、111、114、105、97、97、108、115、80、111、105、110、116 建立的多個字元的字串。

<html>
<head>
<title>JavaScript String fromCodePoint() Method</title>
</head>
<body>
<script>
   const num1 = 84;
   const num2 = 117;
   const num3 = 116;
   const num4 = 111;
   const num5 = 114;
   const num6 = 105;
   const num7 = 97;
   const num8 = 108;
   const num9 = 115;
   const num10 = 80;
   const num11 = 111;
   const num12 = 105;
   const num13 = 110;
   const num14 = 116;
   document.write("Uni code point values are: ", num1, ", ", num2, ", ", num3, ", ", num4, ", ", num5, ", ", num6, ", ", num7, ", ", num7, ", ", num8, ", ", num9, ", ", num10, ", ", num11, ", ", num12, ", ", num13, ", ", num14);
   document.write("<br>Created string: ", String.fromCodePoint(num1, num2, num3, num4, num5, num6, num7, num8, num9, num10, num11, num12, num13, num14));
</script>
</body>
</html>

輸出

執行上述程式後,它將返回一個新的字串“TutorialsPoint”。

Uni code point values are: 84, 117, 116, 111, 114, 105, 97, 97, 108, 115, 80, 111, 105, 110, 116
Created string: TutorialsPoint

示例 3

如果 numN 不是整數,小於零,或大於 0x10FFFF,它將丟擲一個 'RangeError' 異常。

<html>
<head>
<title>JavaScript String fromCodePoint() Method</title>
</head>
<body>
<script>
   const unicodepoint = -2;
   document.write("Uni code point: ", unicodepoint);
   try {
      document.write("Created string: ", String.fromCodePoint(unicodepoint));
   } catch (error) {
      document.write("<br>", error);
   }
</script>
</body>
</html>

輸出

執行上述程式後,它將丟擲一個 'RangeError' 異常。

Uni code point: -2
RangeError: Invalid code point -2

示例 4

在下面給出的示例中,我們在 from 迴圈內使用 fromCodePoint() 方法檢索包含所有大寫字母的字串,這些字母是由依次傳遞給此方法的數字一個一個建立的,迴圈從 65 開始,迭代到 90

<html>
<head>
<title>JavaScript String fromCodePoint() Method</title>
</head>
<body>
<script>
   const unicodepoint_start = 65;
   document.write("Unicode starts at: ", unicodepoint_start);
   document.write("<br>New created string: ");
   for(let i = unicodepoint_start; i<=90; i++){
      document.write(String.fromCodePoint(i));
   }
</script>
</body>
</html>

輸出

以上程式返回一個包含所有大寫字母的字串,如下所示:

Unicode starts at: 65
New created string: ABCDEFGHIJKLMNOPQRSTUVWXYZ
廣告