Skip to content

Instantly share code, notes, and snippets.

@ruseel
Created November 18, 2022 10:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ruseel/968cd6801f803faa54cd7845d2104ac6 to your computer and use it in GitHub Desktop.
Save ruseel/968cd6801f803faa54cd7845d2104ac6 to your computer and use it in GitHub Desktop.
substring java string with emoji
(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