philipbohun.com

Glojure by Example: String Functions

clojure.string covers the everyday string operations. Require it under an alias so the calls do not collide with clojure.core.

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

(println (string/upper-case "Glojure"))
(println (string/lower-case "Glojure"))
(println (string/trim "  hi  "))
(println (string/starts-with? "glojure" "glo"))
(println (string/ends-with? "glojure" "ure"))
(println (string/includes? "glojure" "loj"))

split divides on a regular expression. join assembles a collection. replace substitutes a literal match.

(println (string/split "a,b,c" #","))
(println (string/join " " ["go" "by" "example"]))
(println (string/replace "foo bar foo" "foo" "baz"))

These functions return new strings. They do not modify the input.

$ glj string-functions.glj
GLOJURE
glojure
hi
true
true
true
[a b c]
go by example
baz bar baz

Next example: String Formatting.