Skip to content

Instantly share code, notes, and snippets.

@henryw374
Created November 30, 2021 14:01
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save henryw374/8f1e46bf47404fb3a8cdadadadb24d10 to your computer and use it in GitHub Desktop.
Save henryw374/8f1e46bf47404fb3a8cdadadadb24d10 to your computer and use it in GitHub Desktop.
clojure safe subs substring
(ns com.widdindustries.safe-subs)
(defn subs-safe
"like clojure.core/subs but more forgiving"
([^String s start] (subs-safe s start (count s)))
([^String s start end]
(when s
(let [end (if (> end (count s))
(count s)
end)]
(cond
(zero? start) (subs s start end)
(> start (count s)) ""
:else (subs s start end))))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment