Created
November 18, 2022 10:29
-
-
Save ruseel/968cd6801f803faa54cd7845d2104ac6 to your computer and use it in GitHub Desktop.
substring java string with emoji
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
(import 'java.text.BreakIterator) | |
(defn break-index-seq [s] | |
(let [iter (doto (BreakIterator/getCharacterInstance) | |
(.setText s)) | |
c (.first iter) | |
-seq (fn f [c iter] | |
(lazy-seq (when (not= c BreakIterator/DONE) | |
(cons c (f (.next iter) iter)))))] | |
(-seq c iter))) | |
(defn string-with-emoji-subs [v s e] | |
(let [idxs (break-index-seq v) | |
s (max 0 s) | |
e (min (dec (count idxs)) e)] | |
(.substring v (nth idxs s) (nth idxs e)))) | |
(string-with-emoji-subs "πππ" 0 0) | |
(string-with-emoji-subs "πππ" 0 1) | |
(string-with-emoji-subs "πππ" 0 2) | |
(string-with-emoji-subs "πππ" 0 3) | |
(string-with-emoji-subs "πππ" 0 4) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment