Skip to content

Instantly share code, notes, and snippets.

@davegolland
Created February 16, 2015 21:10
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 davegolland/84ff98c6afce44480da2 to your computer and use it in GitHub Desktop.
Save davegolland/84ff98c6afce44480da2 to your computer and use it in GitHub Desktop.
(defn subs-to
"Like subs, but does not throw if index is out of range"
([s start]
(subs-to s start (count s)))
([s start end]
(if (or (<= (count s) start) (<= end start))
""
(subs s start (min (count s) end)))))
(deftest subs-to
(let [s "Clojure"]
(is-= "o" (string/subs-to s 2 3))
(is-= "lo" (string/subs-to s 1 3))
(is-= "lojure" (string/subs-to s 1)))
(let [s "abcd"]
(is-= "" (string/subs-to s 100))
(is-= "" (string/subs-to s 100 200))
(is-= "cd" (string/subs-to s 2 100))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment