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.
提供大多数标准字符串处理和处理函数
这是在任何通用编程语言中所期望的。
在 Clojure 和 ClojureScript 中,字符串使用原生
平台实现进行表示,并可直接处理,
例如 (.toLowerCase "FOO") ;=> "foo"
。clojure.string
名称空间
让你能够以惯用方式处理字符串
(clojure.string/lower-case "FOO") ;=> "foo"
.
需要记住的一点是这些大多数(所有?)函数将
要作用于的字符串作为第一个参数,非常适合
与 单画眉运算符 (->) 一起使用
,就像在这个刻意设计的示例中
(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”