ClojureDocs

Namespaces

clojure.string

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.core/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"
Vars in clojure.string

b

blank?
True if s is nil, empty, or contains only whitespace.

c

capitalize
Converts first character of the string to upper-case, all other characters to lower-case.

e

escape
Return a new string, using cmap to escape each character ch from s as follows: If (cmap ch) is nil, append ch to the new string. If (cmap ch) is non-nil, append (str (cmap ch)) instead.

j

join
Returns a string of all elements in coll, as returned by (seq coll), separated by an optional separator.

l

lower-case
Converts string to all lower-case.

r

re-quote-replacement
Given a replacement string that you wish to be a literal replacement for a pattern match in replace or replace-first, do the necessary escaping of special characters in the replacement.
replace
Replaces all instance of match with replacement in s. match/replacement can be: string / string char / char pattern / (string or function of match). See also replace-first. The replacement is literal (i.e. none of its characters are treated specially) for all cases above except pattern / string. For pattern / string, $1, $2, etc. in the replacement string are substituted with the string that matched the corresponding parenthesized group in the pattern. If you wish your replacement string r to be used literally, use (re-quote-replacement r) as the replacement argument. See also documentation for java.util.regex.Matcher's appendReplacement method. Example: (clojure.string/replace "Almost Pig Latin" #"\b(\w)(\w+)\b" "$2$1ay") -> "lmostAay igPay atinLay"
replace-first
Replaces the first instance of match with replacement in s. match/replacement can be: char / char string / string pattern / (string or function of match). See also replace. The replacement is literal (i.e. none of its characters are treated specially) for all cases above except pattern / string. For pattern / string, $1, $2, etc. in the replacement string are substituted with the string that matched the corresponding parenthesized group in the pattern. If you wish your replacement string r to be used literally, use (re-quote-replacement r) as the replacement argument. See also documentation for java.util.regex.Matcher's appendReplacement method. Example: (clojure.string/replace-first "swap first two words" #"(\w+)(\s+)(\w+)" "$3$2$1") -> "first swap two words"
reverse
Returns s with its characters reversed.

s

split
Splits string on a regular expression. Optional argument limit is the maximum number of splits. Not lazy. Returns vector of the splits.
split-lines
Splits s on \n or \r\n.

t

trim
Removes whitespace from both ends of string.
trim-newline
Removes all trailing newline \n or return \r characters from string. Similar to Perl's chomp.
triml
Removes whitespace from the left side of string.
trimr
Removes whitespace from the right side of string.

u

upper-case
Converts string to all upper-case.