Case conversion — toUpperCase can change the length¶
Level: 201 · for anyone who compares strings without regard to case
Stub — an outline, not a lesson. There is no runnable example behind this page yet, so nothing on it has been through the check that backs every other claim in this library. The questions below are what the finished page has to answer.
One line: Case mapping follows Unicode's full rules, so it can change length: "straße".toUpperCase() is "STRASSE" and "İ".toLowerCase() is two code units; lowercasing both sides therefore misses that "straße" and "STRASSE" match, which localeCompare with sensitivity: "base" finds.
Keywords: toUpperCase, toLowerCase, toLocaleUpperCase, toLocaleLowerCase
What the finished page will answer¶
- Which characters change a string's length under
toUpperCaseortoLowerCase:ß,fi,ʼn,İ? - What do
toLocaleUpperCaseandtoLocaleLowerCasedo toi,Iandİinen,trandlt? - Why does
"ΣΑΣ".toLowerCase()end inςrather thanσ? - Why is
a.toLowerCase() === b.toLowerCase()not a case-insensitive comparison, and what dolocaleComparewithsensitivity: "base"and"accent"do instead? - Does
"ß".toUpperCase().toLowerCase()give backß?
Examples it will need¶
-
case_conversion_length_js.js— strings whose length changes undertoUpperCaseortoLowerCase(ß,fi,ʼn,İ), with the lengths and code points before and after -
case_conversion_locale_js.js—toLocaleUpperCaseandtoLocaleLowerCaseofi,I,İandÌinen,trandlt, then the Greek final sigma -
case_conversion_compare_js.js— case-insensitive comparison three ways (lowercasing both sides,localeComparewithsensitivity: "base", and with"accent") onstraße/STRASSEandrésumé/RESUME
See also¶
- Comparing and sorting strings —
localeCompareand itssensitivityoption - Normalization — the combining dot that
İleaves, and what NFC does with it - Strings are UTF-16 — why a longer result shows up in
length - Unicode mode — how the
iflag folds case with and withoutu - Encodings: Case is not a per-character operation ↗ — why case mapping cannot be done one letter at a time
- Python: Lowercasing is not folding ↗ — the
casefoldof Python, which JavaScript has no equivalent of - Java text: Case is locale-sensitive ↗ — the Turkish
iproblem in Java, from the default-locale side