Skip to content
Strings
step 1/7

Reading — step 1 of 7

Learn

~2 min readSequences, Strings, Destructuring

Strings in Clojure are Java's String. The clojure.string namespace adds idiomatic functions; the seq abstraction lets you treat strings as character sequences when needed.

Basic operations (Java methods)

(count "hello")               ;; 5
(.toUpperCase "hello")         ;; "HELLO"
(.toLowerCase "HELLO")         ;; "hello"
(.startsWith "hello" "he")     ;; true
(.indexOf "hello" "l")         ;; 2
(subs "hello" 1 4)             ;; "ell"  — 0-indexed, end-exclusive
(str "a" "b" 42)               ;; "ab42"  — concat anything

Clojure preserves Java method calling for Strings. (.method obj args) is interop syntax.

clojure.string namespace

(require '[clojure.string :as str])

(str/upper-case "hello")        ;; "HELLO"
(str/lower-case "HELLO")        ;; "hello"
(str/trim "  hello  ")          ;; "hello"
(str/split "a,b,c" #",")        ;; ["a" "b" "c"]
(str/join "-" ["a" "b" "c"])    ;; "a-b-c"
(str/replace "hello" "l" "L")   ;; "heLLo"
(str/blank? "  ")               ;; true
(str/starts-with? "hello" "he") ;; true
(str/includes? "hello" "ell")   ;; true

The clojure.string ns is preferred over Java method interop in modern code — more functional, more uniform.

Strings as sequences

(seq "hello")                   ;; (\h \e \l \l \o)
(map #(Character/toUpperCase %) "hello")
;; (\H \E \L \L \O)
(apply str (reverse "hello"))   ;; "olleh"

When you seq a string, you get characters (\h is Clojure char syntax). apply str collapses chars/strings back to a string.

Regex

Clojure has first-class regex literals: #"pattern".

(re-find #"\d+" "abc 123 def")     ;; "123"
(re-seq #"\d+" "a 1 b 22 c 333")  ;; ("1" "22" "333")
(re-matches #"\d+" "123")           ;; "123" — must match WHOLE string
(re-matches #"\d+" "123abc")        ;; nil
(str/replace "hello123" #"\d+" "X") ;; "helloX"

Regex literals are JVM Patterns. re-find finds first match; re-seq returns all; re-matches requires full match.

With capture groups:

(re-matches #"(\d+)-(\d+)" "123-456")
;; ["123-456" "123" "456"]

Returns a vector with the full match first, then each group.

String formatting

(format "%s is %d" "Ada" 36)       ;; "Ada is 36"
(format "%.2f" 3.14159)             ;; "3.14"

Uses Java's printf-style formatting. The format function is in clojure.core.

Common mistakes

  • Mutating strings — strings are IMMUTABLE in Java/Clojure. All operations return new strings.
  • Forgetting that subs is end-exclusive(subs "hello" 0 5) is the whole string; (subs "hello" 0 6) throws.
  • Mixing Java methods and clojure.string — both work, but the namespace functions are more idiomatic and curry-friendly.
  • Concatenating in a loop with str — fine for a few values; for many, use a StringBuilder or (apply str coll).
  • Treating a char as a string\h is a character, not a string. Convert with (str \h).

Discussion

Ask a question, share an insight, or help someone who’s stuck.

Sign in to post a comment or reply.

Loading…