Why string.length Lies: Grapheme Clusters and Emoji Length

The Lie Your Runtime Tells You

'👨‍👩‍👧‍👦'.length  // 11

That is not 11 characters. It is one family emoji. The .length property in JavaScript counts UTF-16UTF-16
एक परिवर्तनशील-चौड़ाई वाला Unicode एन्कोडिंग जो प्रति वर्ण 2 या 4 बाइट का उपयोग करता है, JavaScript, Java और Windows द्वारा आंतरिक रूप से उपयोग किया जाता है।
code units, not visible characters. For most ASCII text this distinction is invisible, but emoji expose the gap brutally.

Understanding why this happens — and how to fix it — requires knowing what a grapheme cluster is.

Three Levels of "Character"

Unicode defines multiple levels of text unit, each useful for different purposes:

Level Unit JavaScript Python
Byte Raw storage unit len(s.encode('utf-8UTF-8
एक परिवर्तनशील-चौड़ाई वाला Unicode एन्कोडिंग जो प्रति वर्ण 1 से 4 बाइट का उपयोग करता है, वेब पर प्रमुख (98%+ वेबसाइटों द्वारा उपयोग किया जाता है)।
'))
Code unit Encoding unit s.length
Code point Unicode scalar value [...s].length len(s)
Grapheme cluster User-perceived character Intl.Segmenter grapheme library

A grapheme cluster is what a human thinks of as "one character" — one visible glyph on screen. For emoji, this is always at least one code point, but often several combined.

What Makes an Emoji "Long"?

Basic Emoji: 1 Code Point, 2 Code Units

😀 = U+1F600
UTF-16: 0xD83D 0xDE00 (surrogate pair)
.length in JS: 2
Python len(): 1

Emoji + Variation Selector: 2 Code Points

❤️ = U+2764 + U+FE0F
.length in JS: 2
Python len(): 2
Grapheme clusters: 1

Emoji + Skin Tone: 2 Code Points

👍🏽 = U+1F44D + U+1F3FD
.length in JS: 4 (two surrogate pairs)
Python len(): 2
Grapheme clusters: 1

ZWJज़ीरो विड्थ जॉइनर (ZWJ)
एक अदृश्य Unicode वर्ण (U+200D) जिसका उपयोग कई इमोजी को एक संयुक्त इमोजी में जोड़ने के लिए किया जाता है, जैसे लोगों और वस्तुओं को पेशे वाले इमोजी में संयोजित करना।
Sequence: 3+ Code Points

👩‍💻 = U+1F469 + U+200D + U+1F4BB
.length in JS: 5
Python len(): 3
Grapheme clusters: 1

Family Emoji: 7 Code Points

👨‍👩‍👧‍👦 = 👨 + ZWJ + 👩 + ZWJ + 👧 + ZWJ + 👦
.length in JS: 11
Python len(): 7
Grapheme clusters: 1

Correct Counting in JavaScript

Using Intl.Segmenter (Modern, Built-In)

Intl.Segmenter is available in all modern browsers and Node.js 16+. It segments text by grapheme clusters:

function countGraphemes(str) {
  const segmenter = new Intl.Segmenter();
  return [...segmenter.segment(str)].length;
}

countGraphemes('Hello')     // 5 — correct
countGraphemes('Hello 😀') // 7 — correct
countGraphemes('👨‍👩‍👧‍👦')   // 1 — correct!
countGraphemes('👩🏽‍💻')    // 1 — correct!

Slicing Text Correctly

Slicing with .slice() on emoji strings corrupts surrogate pairs:

const s = 'Hello 👋';

// WRONG: may cut through a surrogate pair
s.slice(0, 7)  // 'Hello \uD83D' — broken!

// CORRECT: spread to code points first
[...s].slice(0, 7).join('')  // 'Hello 👋' — correct

// EVEN BETTER: slice by grapheme clusters
function sliceByGrapheme(str, start, end) {
  const segmenter = new Intl.Segmenter();
  const segments = [...segmenter.segment(str)];
  return segments.slice(start, end).map(s => s.segment).join('');
}

sliceByGrapheme('Hello 👋 World', 0, 7)  // 'Hello 👋'

String Reversal

Reversing an emoji string with .split('').reverse().join('') produces garbage:

// WRONG: reverses code units, breaks surrogates
'Hello 😀'.split('').reverse().join('')  // '😀 olleH' (actually garbled)

// CORRECT: reverse by code points
[...'Hello 😀'].reverse().join('')  // '😀 olleH'

// BEST: reverse by grapheme clusters
function reverseGraphemes(str) {
  const segmenter = new Intl.Segmenter();
  return [...segmenter.segment(str)]
    .map(s => s.segment)
    .reverse()
    .join('');
}

reverseGraphemes('Hello 👨‍👩‍👧‍👦')  // '👨‍👩‍👧‍👦 olleH'

Correct Counting in Python

Python 3's len() counts code points, not grapheme clusters. For most ASCII text this is identical to grapheme count, but for emoji it is not.

text = '👨‍👩‍👧‍👦'
len(text)  # 7 — code points (wrong for "visible characters")

Using the grapheme Library

import grapheme

grapheme.length('👨‍👩‍👧‍👦')   # 1 — correct
grapheme.length('Hello 😀')  # 7 — correct

# Slice by grapheme clusters
grapheme.slice('Hello 👨‍👩‍👧‍👦', 0, 7)  # 'Hello 👨‍👩‍👧‍👦'

# Iterate graphemes
list(grapheme.graphemes('Hi 👋'))
# ['H', 'i', ' ', '👋']

Using the regex Module with \X

The \X pattern in the regex module matches grapheme clusters:

import regex

def count_graphemes(text):
    return len(regex.findall(r'\X', text))

count_graphemes('👨‍👩‍👧‍👦')   # 1
count_graphemes('Hello 👨‍👩‍👧‍👦')  # 7

Other Languages

Java

// Java uses UTF-16 internally like JavaScript
"👨‍👩‍👧‍👦".length()  // 11 (code units)

// Count code points
"👨‍👩‍👧‍👦".codePointCount(0, "👨‍👩‍👧‍👦".length())  // 7

// Count grapheme clusters (requires ICUICU (ICU)
International Components for Unicode — एक व्यापक रूप से उपयोग की जाने वाली ओपन-सोर्स लाइब्रेरी जो इमोजी प्रोसेसिंग सहित Unicode और अंतर्राष्ट्रीयकरण समर्थन प्रदान करती है।
) BreakIterator bi = BreakIterator.getCharacterInstance(); bi.setText("👨‍👩‍👧‍👦"); int count = 0; while (bi.next() != BreakIterator.DONE) count++; // count = 1

Swift

Swift's String is designed around grapheme clusters — count returns grapheme count by default:

"👨‍👩‍👧‍👦".count  // 1 — correct out of the box!

This is one area where Swift's string model is superior to most other languages.

Rust

// Rust len() returns bytes
"👨‍👩‍👧‍👦".len()  // 25 (bytes)

// chars() iterates code points
"👨‍👩‍👧‍👦".chars().count()  // 7

// For grapheme clusters, use the unicode-segmentation crate
use unicode_segmentation::UnicodeSegmentation;
"👨‍👩‍👧‍👦".graphemes(true).count()  // 1

Practical Implications

Input Validation / Character Limits

If you show users "max 280 characters" and count by .length in JavaScript, users can enter far fewer emoji than expected (or you silently truncate their input). Count by grapheme clusters for user-facing limits.

Database Column Sizing

Ensure column size limits account for multi-code-point emoji. A VARCHAR(10) that accepts emoji-heavy input needs to be measured in code points (PostgreSQL) or bytes (MySQL with utf8mb4), not graphemes.

Text Rendering

Layout engines that use pixel widths handle emoji correctly by nature — they measure bounding boxes, not code units. But manual text layout (canvas, PDF generation, terminal output) needs grapheme-aware iteration.

Use our Sequence Analyzer to see exactly how many code points, code units, and grapheme clusters any emoji string contains — making these abstract concepts concrete.

संबंधित टूल्स

🔍 सीक्वेंस विश्लेषक सीक्वेंस विश्लेषक
ZWJ सीक्वेंस, स्किन टोन मॉडिफ़ायर, कीकैप सीक्वेंस और फ्लैग जोड़ों को अलग-अलग घटकों में डीकोड करें।

शब्दकोश के शब्द

ICU (ICU) ICU (ICU)
International Components for Unicode — एक व्यापक रूप से उपयोग की जाने वाली ओपन-सोर्स लाइब्रेरी जो इमोजी प्रोसेसिंग सहित Unicode और अंतर्राष्ट्रीयकरण समर्थन प्रदान करती है।
UTF-16 UTF-16
एक परिवर्तनशील-चौड़ाई वाला Unicode एन्कोडिंग जो प्रति वर्ण 2 या 4 बाइट का उपयोग करता है, JavaScript, Java और Windows द्वारा आंतरिक रूप से उपयोग किया जाता है।
UTF-8 UTF-8
एक परिवर्तनशील-चौड़ाई वाला Unicode एन्कोडिंग जो प्रति वर्ण 1 से 4 बाइट का उपयोग करता है, वेब पर प्रमुख (98%+ वेबसाइटों द्वारा उपयोग किया जाता है)।
इमोजी इमोजी
एक जापानी शब्द (絵文字) जिसका अर्थ है 'चित्र वर्ण' — छोटे ग्राफिकल प्रतीक जो डिजिटल संचार में विचार, भावनाएं और वस्तुएं व्यक्त करने के लिए उपयोग किए जाते हैं।
कोड पॉइंट कोड पॉइंट
Unicode मानक में प्रत्येक वर्ण को दिया गया एक अद्वितीय संख्यात्मक मान, जो U+XXXX प्रारूप में लिखा जाता है (जैसे 😀 के लिए U+1F600)।
कोड यूनिट कोड यूनिट
किसी वर्ण को एन्कोड करने के लिए उपयोग की जाने वाली न्यूनतम बिट संयोजन: UTF-8 के लिए 8-बिट, UTF-16 के लिए 16-बिट, और UTF-32 के लिए 32-बिट।
ग्राफ़ीम क्लस्टर ग्राफ़ीम क्लस्टर
एक उपयोगकर्ता-दृश्य वर्ण जो कई Unicode कोड पॉइंट से मिलकर बना हो सकता है, लेकिन एकल दृश्य इकाई के रूप में प्रदर्शित होता है।
ज़ीरो विड्थ जॉइनर (ZWJ) ज़ीरो विड्थ जॉइनर (ZWJ)
एक अदृश्य Unicode वर्ण (U+200D) जिसका उपयोग कई इमोजी को एक संयुक्त इमोजी में जोड़ने के लिए किया जाता है, जैसे लोगों और वस्तुओं को पेशे वाले इमोजी में संयोजित …
यूनिकोड यूनिकोड
एक सार्वभौमिक वर्ण एन्कोडिंग मानक जो इमोजी सहित सभी लेखन प्रणालियों और प्रतीक सेटों में प्रत्येक वर्ण को एक अद्वितीय संख्या प्रदान करता है।
वेरिएशन सिलेक्टर (VS) वेरिएशन सिलेक्टर (VS)
Unicode वर्ण (VS-15 U+FE0E और VS-16 U+FE0F) जो यह निर्धारित करते हैं कि कोई वर्ण टेक्स्ट (मोनोक्रोम) या इमोजी (रंगीन) प्रस्तुति में रेंडर होगा।
सरोगेट पेयर सरोगेट पेयर
दो UTF-16 कोड यूनिट (एक हाई सरोगेट U+D800-U+DBFF और उसके बाद एक लो सरोगेट U+DC00-U+DFFF) जो मिलकर U+FFFF से ऊपर के किसी वर्ण का प्रतिनिधित्व करते हैं।

संबंधित लेख