JavaScript 字串 codePointAt() 方法



JavaScript 字串 codePointAt() 方法返回一個非負整數,表示從指定索引開始的字串中字元的 Unicode 碼位。如果給定的索引不在0 到 str.length-1的範圍內,則該方法返回“undefined”作為結果。

此方法返回字串字元和圖示(表情符號、符號等)的 Unicode 碼位值。

語法

以下是 JavaScript 字串 codePointAt() 方法的語法:

codePointAt(index)

引數

此方法接受一個名為“index”的引數,如下所述:

  • index - 要返回的字元的基於零的索引(位置)。

返回值

此方法返回一個非負整數,表示給定索引處字元的 Unicode 碼位值。

示例 1

透過將索引值傳遞為0來檢索字串中第一個字元的 Unicode 碼位。

在以下示例中,我們使用 JavaScript 字串 codePointAt() 方法來檢索給定字串“Tutorials Point”中指定索引0處的字元的 Unicode 碼位值。

<html>
<head>
<title>JavaScript String codePointAt() Method</title>
</head>
<body>
<script>
   const str = "Tutorials Point";
   document.write("String: ", str);
   const index = 0;
   document.write("<br>Index: ", index);
   document.write("<br>The Uni code point value of the first character '", str.at(index), "' is: ", str.codePointAt(index));
</script>
</body>
</html>

輸出

以上程式返回字串“Tutorials Point”第一個字元“T”的 Unicode 碼位值為 84。

String: Tutorials Point
Index: 0
The Uni code point value of the first character 'T' is: 84

示例 2

檢索字串中最後一個字元的 Unicode 碼位。

以下是 JavaScript 字串 codePointAt() 方法的另一個示例。我們使用此方法來檢索給定字串“Hello World”中指定索引str.length-1處的字元的 Unicode 碼位,它是最後一個元素。

<html>
<head>
<title>JavaScript String codePointAt() Method</title>
</head>
<body>
<script>
   const str = "Hello World";
   document.write("String: ", str);
   const index = str.length-1;
   document.write("<br>Index: ", index);
   document.write("<br>The Uni code point value of the last character '", str.at(-1), "' is: ", str.codePointAt(index));
</script>
</body>
</html>

輸出

執行以上程式後,它將返回字元“d”的 Unicode 值 100。

String: Hello World
Index: 10
The Uni code point value of the last character 'd' is: 100

示例 3

如果 index 引數值不在0 到 str.length-1的範圍內,它將返回undefined

在此示例中,我們使用 JavaScript 字串 codePointAt() 方法來檢索字串“JavaScript”中指定索引-3處的字元的 Unicode 碼位,它超出了0 - str.length-1的範圍。

<html>
<head>
<title>JavaScript String codePointAt() Method</title>
</head>
<body>
<script>
   const str = "JavaScript";
   document.write("String: ", str);
   const index = -3;
   document.write("<br>Index: ", index);
   document.write("<br>The Uni code point value of character the specified index ", index, " is: ", str.codePointAt(index));
</script>
</body>
</html>

輸出

執行以上程式後,它將返回“undefined”。

String: JavaScript
Index: -3
The Uni code point value of character the specified index -3 is: undefined

示例 4

圖示(表情符號)的 Unicode 碼位值。

在此程式中,codePointAt() 方法用於檢索圖示的 Unicode 碼位值。

<html>
<head>
<title>JavaScript String codePointAt() Method</title>
</head>
<body>
<script>
   document.write("Character(icon): ", "😀", ", Uni code point: ", "😀".codePointAt(0));
</script>
</body>
</html>

輸出

以上程式返回圖示“😀”的 Unicode 碼位為“128512” -

Character(icon): 😀, Uni code point: 128512

示例 5

在給定的程式中,我們正在 for迴圈 內使用 codePointAt() 方法來檢索字串 "Welcome to Tutorials Point" 中所有字元的 Unicode 碼點。

<html>
<head>
<title>JavaScript String codePointAt() Method</title>
</head>
<body>
<script>
   const str = "Welcome to Tutorials Point";
   document.write("String: ", str);
   document.write("<br>Uni code point value of all characters:<br>")
   for(let i = 0; i<str.length; i++){
      document.write("Character: ", str.at(i), ", Uni code point: ", str.charCodeAt(i), "<br>");
   }
</script>
</body>
</html>

輸出

上述程式返回所有字元的 Unicode 碼點。

String: Welcome to Tutorials Point
Uni code point value of all characters:
Character: W, Uni code point: 87
Character: e, Uni code point: 101
Character: l, Uni code point: 108
Character: c, Uni code point: 99
Character: o, Uni code point: 111
Character: m, Uni code point: 109
Character: e, Uni code point: 101
Character: , Uni code point: 32
Character: t, Uni code point: 116
Character: o, Uni code point: 111
Character: , Uni code point: 32
Character: T, Uni code point: 84
Character: u, Uni code point: 117
Character: t, Uni code point: 116
Character: o, Uni code point: 111
Character: r, Uni code point: 114
Character: i, Uni code point: 105
Character: a, Uni code point: 97
Character: l, Uni code point: 108
Character: s, Uni code point: 115
Character: , Uni code point: 32
Character: P, Uni code point: 80
Character: o, Uni code point: 111
Character: i, Uni code point: 105
Character: n, Uni code point: 110
Character: t, Uni code point: 116
廣告