JavaScript 字符串 fromCodePoint() 方法
JavaScript 字符串的 fromCodePoint() 方法返回由指定代码点值序列创建的字符串。您可以向此方法传递一个或多个 Unicode 代码点值。如果传递的值不是整数、小于 0(或为负数)或大于 0x10FFFF,则会抛出 RangeError 异常。此方法是 静态 的,可以使用类语法(例如 String.fromCodePoint())调用,而不是在变量上调用。
Unicode 代码点是与 Unicode 标准中的特定字符对应的数值。每个字符和符号都有其独特的 Unicode 代码点值。
语法
以下是 JavaScript 字符串 fromCodePoint() 方法的语法 -
String.fromCodePoint(num1, num2, /*..., */ numN)
参数
此方法接受多个相同类型的参数,例如"num1"、"num2"、"num3"至"numN",如下所述 -
- num1, num2....numN - 它是一个整数值,表示 [0, 0x10FFFF] 范围内的 Unicode 代码点。
返回值
此方法返回由指定代码点序列创建的字符串。
示例 1
如果我们将单个 Unicode 代码点值传递给此方法,它将返回仅包含一个字符的字符串。
在下面的示例中,我们使用 JavaScript 字符串 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 字符串 fromCodePoint() 方法的另一个示例。我们使用此方法检索由指定 Unicode 序列 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

