Get Unicode-aware length of string in JavaScript
stringInstance.length
in JavaScript is not Unicode aware.
That means, for example, that the following code:
"š¤".length
Will return 2, not 1.
If you want a Unicode-aware string length, use this function:
function unicodeLength(str) {
return [...str].length
}
Using that function, the following code:
unicodeLength("š¤")
Will return 1.
References
- The function is based on the method by daxim in his response to the question Getting a string length that contains unicode character exceeding 0xffff on Stack Overflow.