Loading...
const text = '👍🏽 ok';
const count = text.length; // 7const text = '👍🏽 ok';
const segmenter = new Intl.Segmenter();
const count = [...segmenter.segment(text)].length; // 4length counts UTF-16 code units; Intl.Segmenter counts the characters a user actually sees.
slice() and breaking an emoji or accented letter in half.Intl.Segmenter, not by indexes, when truncating or counting.normalize() when they come from different sources.Unicode assigns a number to each character; UTF-8 is one way to store those numbers as bytes.
JavaScript strings are UTF-16, and characters outside the basic range take two code units, called a surrogate pair.
The unique number Unicode gives a character, written like 'U+00E9' for 'é'. Some visible characters combine several code points.