Clojure: String
"some string abc"
to join string (concatenate), use str
.
;; join string (str "aa" "bb") ; "aabb" (str 3 " cats") ; "3 cats"
substring
(subs str start_index)
(subs str start_index end_index)
Index start at 0. Start index is inclusive. End index is exclusive.
;; get substring, starting at index 1. (subs "abcd" 1) ; "bcd" ;; get substring, with start/end indexes. (subs "abcd" 1 3) ; "bc"
Clojure string functions
For string functions, use the Clojure library clojure.string
.
Example of calling a function in clojure.string
namespace:
; calling function upper-case from namespace clojure.string (clojure.string/upper-case "bb") ; BB
Note: slash is used to separate name space.
Call Java's String Methods
Clojure itself doesn't have many string functions. It is common to call Java's methods in Clojure.
To call Java's string method, use the syntax
(. java_object method_name args)
For example, there's Java String method named charAt
. To use it, do:
;; call Java method on object (. "abc" charAt 1) ; \b
A more commonly used syntax to call Java method is
(.method_name java_object args)
(.charAt "abc" 1) ; \b