clojure.string/split¶
Splits string on a regular expression. Optional argument limit is the maximum number of parts. Not lazy. Returns vector of the parts. Trailing empty strings are not returned - pass limit of -1 to return all.
Examples¶
Note by Jacolyte¶
user=> (clojure.string/split "foo bar") java.lang.ClassNotFoundException: clojure.string
Do I need to require / use anything?
Note by rustem.suniev¶
You need to add a namespace: user => (use 'clojure.string)
Note by cocoOS¶
I 've a doubt...in ruby if I've this:
bignumber="2938434"
I can do this
bignumber.split("")
and get ("2", "9", "3", "8" .....)
How can I do this with clojure?...thanks
Note by Domon¶
Hi, cocoOS. You may want to do it like this:
user=> (use '[clojure.string :only (split)]) nil user=> (def bignumber "2938434") #'user/bignumber user=> (split bignumber #"") ["" "2" "9" "3" "8" "4" "3" "4"]
You will have to filter out the empty string.
But there are other ways, like:
user=> (map str (vec bignumber))
("2" "9" "3" "8" "4" "3" "4")
Note by Iceland_jack¶
user=> (seq "2938434") (\2 \9 \3 \8 \4 \3 \4)
might also be what you want.
Note by Instinct212¶
Thanks for the help Domon and Iceland_jack, one more question:
Say I have used sequence in the way:
user=> (seq "2938434") (\2 \9 \3 \8 \4 \3 \4)
If I only wanted to access the 4th item in the sequence (which is 8), how would I do so? Aside from using (next (next (next ...))) ? Just trying to index it giving that I want position 4
Note by olivergeorge¶
When limit=1 the original string is returned
(clojure.string/split "abcd" #"a" 1) => ["abcd"] (clojure.string/split "abcd" #"a" 2) => ["" "bcd"]
Note by Yuhta¶
@Instinct212
(nth (seq "2938434") 3) => \8
Note by rux¶
There is a change in behaviour between Java 7 and Java 8 that can affect the returned values. "When there is a positive-width match at the beginning of this string then an empty leading substring is included at the beginning of the resulting array. A zero-width match at the beginning however never produces such empty leading substring." was added to the docs for split().
Both run with Clojure 1.8, the code below splits a string based on capital letters, but...
in Java 7
(clojure.string/split "PrimaryEmailAddr" #"(?=[A-Z])") => ["" "Primary" "Email" "Addr"]
in Java 8
(clojure.string/split "PrimaryEmailAddr" #"(?=[A-Z])") => ["Primary" "Email" "Addr"]
See also¶
- clojure.core/subs (3)
- clojure.string/replace (8)
- clojure.core/re-seq (4)
- clojure.string/split-lines (2)
- clojure.string/trim (0)
- clojure.string/join (4)
Content from the matching ClojureDocs page, with authors credited on each contribution.