Text tools
Count text length accurately
Choose bytes, codepoints or grapheme clusters for the real requirement.
1. Identify the limit
Storage and protocols may limit bytes; visual text fields may count graphemes. These quantities are different.
2. Compare examples
Precomposed é uses one codepoint and two UTF-8 bytes; decomposed e plus acute uses two codepoints but often one grapheme.
3. Pick the tool
Use the unit required by the receiving system. Do not assume its definition of character count.
4. Test boundaries
Include emoji sequences, combining marks and newlines. Never truncate at an arbitrary byte boundary in UTF-8.
Compare UTF-8 units in a real input
Enter é and then e followed by a combining acute accent. The first is one Unicode scalar and two UTF-8 bytes; the second is two scalars and three UTF-8 bytes. Both are normally one grapheme cluster. Try the emoji 👩💻, which includes a joiner and is commonly one grapheme despite multiple scalars and bytes.
Common pitfall
Use the counter that matches the actual storage or user-interface requirement. JavaScript string length counts UTF-16 code units, which is yet another measure. Do not split UTF-8 or a grapheme at an arbitrary boundary.