-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[tip] 姓名头像 #7
Comments
Unicode 表情与编码互转 "😁".codePointAt(0).toString(16); // "1f601"
String.fromCodePoint(0x1f601); // "😁"
let euro = '€';
let love = '❤';
euro.length; // => 1: this character has one 16-bit element
love.length; // => 2: UTF-16 encoding of ❤ is "\ud83d\udc99"
—— The Definitive Guide, Seventh Edition Array.from('😁😁不错哟'); // => ["😁", "😁", "不", "错", "哟"]
[...'😁😁不错哟']; // => ["😁", "😁", "不", "错", "哟"]
for (const str of '😁😁不错哟') {
console.log(str);
}
// 😁
// 😁
// 不
// 错
// 哟 由于js的字符串方法一般操作的是16位值,所以需要对含有Unicode字符的特殊字符串特别处理。 |
截取姓名某一段字符,一般使用
substr
、substring
、slice
之类的方法。但对于Unicode表情,它们的长度一般为2甚至更多,会导致截取到的部分不满足需求。
如
'😁😁不错哟'.substr(0, 3)
返回"😁�"
。使用
Array.from
将字符串转为数组。如Array.from('😁😁不错哟')
返回["😁", "😁", "不", "错", "哟"]
,然后再使用slice
取需要的位数,使用join
拼接。The text was updated successfully, but these errors were encountered: