How To Get The Correct Element From A Unicode String?
Solution 1:
In Javascript, a string is a sequence of 16-bit code points. Since these characters are encoded above the Basic Multilingual Plane, it means that they are represented by a pair of code points, also known as a surrogate pair.
Unicode number of 𝖆
is U+1D586
. And 0x1D586 is greater than 0xFFFF (2^16). So, 𝖆
is represented by a pair of code points, also known as a surrogate pair
console.log("𝖆".length)
console.log("𝖆" === "\uD835\uDD86")
One way is to create an array of characters using the spread syntax or Array.from()
and then get the index you need
var handwriting = `𝖆𝖇𝖈𝖉𝖊𝖋𝖌𝖍𝖎𝖏𝖐𝖑𝖒𝖓𝖔𝖕𝖖𝖗𝖘𝖙𝖚𝖛𝖜𝖝𝖞𝖟𝕬𝕭𝕮𝕯𝕰𝕱𝕲𝕳𝕴𝕵𝕶𝕷𝕸𝕹𝕺𝕻𝕼𝕽𝕾𝕿𝖀𝖁𝖂𝖃𝖄𝖅1234567890`console.log([...handwriting][3])
console.log(Array.from(handwriting)[3])
Solution 2:
A unicode character looks like '\u00E9' so if your string is longer this is normal. To have the real length of a unicode string, you have to convert it to an array :
let charArray = [...handwriting]
console.log(charArray.length) //=62
Each item of your array is a char of your string. charArray[3] will return you the unicode char corresponding to '𝖉'
Post a Comment for "How To Get The Correct Element From A Unicode String?"