gpt4 book ai didi

javascript - 如何遍历所有 Unicode 字符?

转载 作者:行者123 更新时间:2023-11-29 16:09:01 26 4
gpt4 key购买 nike

是否可以遍历所有 Unicode 字符 (UTF-8)?谢谢!我试过使用:

character = String.fromCharCode(i);

但我不确定如何实现它。

最佳答案

UTF-8 是一种编码! JavaScript 字符串是 (mostly)以 UTF-16 编码。仅当您在不支持 ES6 的 String.fromCodePoint 的环境中工作时,编码才重要。 .使用 ES6 从代码点获取字符串:

var s = String.fromCodePoint(codePoint);

没有 ES6,使用 UTF-16 surrogate pair对于字符 U+10000 及以后:

var s;

if (codePoint < 0x10000) {
s = String.fromCharCode(codePoint);
} else {
var offset = codePoint - 0x10000;
s = String.fromCharCode(0xd800 + (offset >> 10),
0xdc00 + (offset & 0x3ff));
}

代码点范围从 U+0000 到 U+10FFFF(1 114 112 值),但并非该范围内的所有内容都是有效的 Unicode 字符。你可以从 http://www.unicode.org/Public/8.0.0/ucd/UnicodeData.txt 得到一张表并提取您真正想要迭代的字符。

关于javascript - 如何遍历所有 Unicode 字符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33792050/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com