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?"