Clojure String utilities It is poor form to (:use clojure.string). Instead, use require with :as to specify a prefix, e.g. (ns your.namespace.here (:require [clojure.string :as str])) Design notes for clojure.string: 1. Strings are objects (as opposed to sequences). As such, the string being manipulated is the first argument to a function; passing nil will result in a NullPointerException unless documented otherwise. If you want sequence-y behavior instead, use a sequence. 2. Functions are generally not lazy, and call straight to host methods where those are available and efficient. 3. Functions take advantage of String implementation details to write high-performing loop/recurs instead of using higher-order functions. (This is not idiomatic in general-purpose application code.) 4. When a function is documented to accept a string argument, it will take any implementation of the correct *interface* on the host platform. In Java, this is CharSequence, which is more general than String. In ordinary usage you will almost always pass concrete strings. If you are doing something unusual, e.g. passing a mutable implementation of CharSequence, then thread-safety is your responsibility.
Provides most standard string manipulation and processing function
that you’d expect in any general-purpose programming language.
In Clojure and ClojureScript strings are represented using the native
platform implementation, and can be directly manipulated,
e.g. (.toLowerCase "FOO") ;=> "foo"
. The clojure.string
namespace
gives you the ability to manipulate strings in an idiomatic way:
(clojure.string/lower-case "FOO") ;=> "foo"
.
Something to keep in mind is most (all?) of these functions take the
string to act on as the first parameter, lending themselves well for
use with the single-thrush operator (->)
, as in this contrived example:
(require '[clojure.string :as str])(-> " .LIRpa ni yAD dloc thgIrb a sAw Ti "
str/reverse
str/trim
str/lower-case
(str/replace #“\s+” " ")
str/capitalize);;=> “It was a bright cold day in april”