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"的 Uni 代码点值为 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"的 uni 码值为 100。
String: Hello World Index: 10 The Uni code point value of the last character 'd' is: 100
示例 3
如果索引参数值不在 0 到 str.length-1 的范围内,则返回 undefined。
在此示例中,我们使用 JavaScript 字符串的 codePointAt() 方法检索字符串"JavaScript"中指定索引 -3 处字符的 uni 代码点,该索引超出了 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>
输出
上述程序返回图标"😀"的 Uni 代码点为"128512"−
Character(icon): 😀, Uni code point: 128512
示例 5
在给定的程序中,我们在 for 循环 中使用 codePointAt() 方法来检索字符串"Welcome to Tutorials Point"中所有字符的 uni 代码点。
<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>
输出
上述程序返回所有字符的 uni 代码点:
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

