将数字转换为 Unicode 符号
我必须把从 A 到 Z 的字母分别打印出来。所以我尝试了以下方法:
for(var i = 65; i < 91; i++) {
$('#alphabet').append('<div class="letter">' + '%' + i + '</div>');
}
我的想法是使用字母的十进制数字(例如:65 - A)通过循环轻松打印它们。这是可能的还是我必须使用数组?
此致。
I have to print out the letters from A to Z each for itself. So I tried the following:
for(var i = 65; i < 91; i++) {
$('#alphabet').append('<div class="letter">' + '%' + i + '</div>');
}
My idea is to use the decimal numbers of the letters (for example: 65 - A) to easily print them via loop. Is this possible or do I have to use an array?
Best regards.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以使用 String.fromCharCode 将字符代码转换为字符串。
You can use String.fromCharCode to convert a character code to string.
对于这种用途(打印从 A 到 Z 的字母),
String.fromCharCode
就足够了;但是,问题标题指定了 Unicode。如果您想要转换 Unicode 代码(如果您是通过搜索来到这里的,则可能会如此),您需要使用String.fromCodePoint
。请注意,此功能是 ES6/ES2015 中的新功能,您可能需要使用转译器或 polyfill 才能在旧版浏览器中使用它。
For this use (printing letters from A to Z),
String.fromCharCode
is sufficient; however, the question title specifies Unicode. If you are looking to convert Unicode codes (and if you came here from search, you might), you need to useString.fromCodePoint
.Note, this function is new to ES6/ES2015, and you may need to use a transpiler or polyfill to use it in older browsers.
String.fromCharCode
String.fromCharCode(...codes: number[]): string
属于 UTF-16 代码单元的数字序列。因此,对于大于 0xFFFF 的 Unicode 代码点,代理对 (
U+需要 D800 到 U+DFFF(代理)
来显示它们,您可以使用以下函数来实现此目的:
String.fromCharCode
String.fromCharCode(...codes: number[]): string
A sequence of numbers that are UTF-16 code units.Therefore, for Unicode code points greater than 0xFFFF, surrogate pairs (
U+D800 to U+DFFF (surrogates)
are needed to display them.You can use the following function to achieve this: