fromCharCode( ) 方法返回指定的 Unicode 编码对应的字符。
语法格式:
String.fromCharCode(n1, n2, ...)
参数:
n1,n1,..表示 指定的Unicode 编码。
示例:
(1)返回指定Unicode 编码的字符:
<!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>Document</title> </head> <body> <script> var char1 = String.fromCharCode(97); var char2 = String.fromCharCode(98); var char3 = String.fromCharCode(99); var char4 = String.fromCharCode(100); var char5 = String.fromCharCode(101); console.log(char1,char2,char3,char4,char5); </script> </body> </html>
控制台输出:
(2)将 Unicode 编码转换为一个字符串:
<!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>Document</title> </head> <body> <script> var str = String.fromCharCode(74,97,121,67,104,111,117); console.log(str); </script> </body> </html>
控制台输出:
可见,我们可以使用该方法生成一个新的字符串。
标签:编码,String,JavaScript,Unicode,var,n1,方法,fromCharCode From: https://www.cnblogs.com/jijm123/p/17380886.html