Last active
April 22, 2026 18:24
-
-
Save romellem/5820f1aeab9695ac763a0a114ea28936 to your computer and use it in GitHub Desktop.
JS String Slice - UTF-8, UTF-16, UTF-32, and Graphemes
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function utf16slice(str, start, end) { | |
| // UTF-16 code units - same as native | |
| return str.slice(start, end); | |
| } | |
| function utf8slice(str, start, end) { | |
| // UTF-8 bytes; slicing mid-codepoint yields U+FFFD | |
| const bytes = new TextEncoder().encode(str); | |
| return new TextDecoder().decode(bytes.slice(start, end)); | |
| } | |
| function utf32slice(str, start, end) { | |
| // Unicode code points (surrogate pairs count as one) | |
| return Array.from(str).slice(start, end).join(''); | |
| } | |
| function graphemeSlice(str, start, end) { | |
| // Visual graphemes — handles emoji ZWJ sequences, flags, combining marks, etc. | |
| const segmenter = new Intl.Segmenter(undefined, { granularity: 'grapheme' }); | |
| const graphemes = Array.from(segmenter.segment(str), s => s.segment); | |
| return graphemes.slice(start, end).join(''); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment