You're viewing version 1.2.0 of split. The latest stable version of Clojure Core is 1.3.0.
1.2.0 Arrow_down_16x16
  • (split s re)
  • (split s re limit)
Splits string on a regular expression. Optional argument limit is
the maximum number of splits. Not lazy. Returns vector of the splits.

6 Examples top

  • user=> (clojure.string/split "q1w2e3r4t5y6u7i8o9p0" #"\d+")
    ["q" "w" "e" "r" "t" "y" "u" "i" "o" "p"]
    user=> (clojure.string/split "q1w2e3r4t5y6u7i8o9p0" #"\d+" 5)
    ["q" "w" "e" "r" "t5y6u7i8o9p0"]
  • ;; Splits a string on space character and joins 
    ;; the resulting collection with a line feed character
    (use '[clojure.string :only (join split)])
    user=> (println
             (join "\n"
               (split "The Quick Brown Fox" #"\s")))
  • (use '[clojure.string :only (split triml)])
    ;; Splitting on whitespace is a common desire.
    user=> (split "Some words to split" #"\s+")
    ["Some" "words" "to" "split"]
    ;; By using the pattern #"\s+", we split on all occurrences of one or
    ;; more consecutive whitespace characters.
    user=> (split "Some    words   with\tother whitespace      \n" #"\s+")
    ["Some" "words" "with" "other" "whitespace"]
    ;; If you are used to Perl's special behavior of split(' ', $str),
    ;; where it ignores leading whitespace in the string to be split, this
    ;; does not quite do it.
    user=> (split "   Leading whitespace causes empty first string" #"\s+")
    ["" "Leading" "whitespace" "causes" "empty" "first" "string"]
    ;; This will do it.
    user=> (defn perl-split-on-space [s]
             (split (triml s) #"\s+"))
    user=> (perl-split-on-space "   This is often what you want   ")
    ["This" "is" "often" "what" "you" "want"]
    ;; There might be cases where you want this instead.
    user=> (split "Some    words   with\tother whitespace      \n" #"\s")
    ["Some" "" "" "" "words" "" "" "with" "other" "whitespace"]
  • (use '[clojure.string :only (split)])
    ;; Split on every occurrence of : character
    user=> (split "root:*:0:0:admin:/var/root:/bin/sh" #":")
    ["root" "*" "0" "0" "admin" "/var/root" "/bin/sh"]
    ;; Empty strings are returned when two colons appear consecutively in
    ;; the string to be split.
    user=> (split "root::0:0::/var/root:/bin/sh" #":")
    ["root" "" "0" "0" "" "/var/root" "/bin/sh"]
    ;; Without specifying a limit, any empty strings at the end are
    ;; omitted.
    user=> (split "root::0:0:admin:/var/root:" #":")
    ["root" "" "0" "0" "admin" "/var/root"]
    user=> (split "root::0:0:admin::" #":")
    ["root" "" "0" "0" "admin"]
    ;; If you want all of the fields, even trailing empty ones, use a
    ;; negative limit.
    user=> (split "root::0:0:admin:/var/root:" #":" -1)
    ["root" "" "0" "0" "admin" "/var/root" ""]
    user=> (split "root::0:0:admin::" #":" -1)
    ["root" "" "0" "0" "admin" "" ""]
    ;; Use a positive limit of n to limit the maximum number of strings in
    ;; the return value to n.  If it returns exactly n strings, the last
    ;; contains everything left over after splitting off the n-1 earlier
    ;; strings.
    user=> (split "root::0:0:admin:/var/root:" #":" 2)
    ["root" ":0:0:admin:/var/root:"]
    user=> (split "root::0:0:admin:/var/root:" #":" 3)
    ["root" "" "0:0:admin:/var/root:"]
    user=> (split "root::0:0:admin:/var/root:" #":" 4)
    ["root" "" "0" "0:admin:/var/root:"]
    user=> (split "root::0:0:admin:/var/root:" #":" 15)
    ["root" "" "0" "0" "admin" "/var/root" ""]
  • (:require [clojure.string :as cstr])
    (def legal-ref "1321-61")
    (cstr/split legal-ref #"-")
    ["1321" "61"]
  • ;; Note: See clojure.core/subs for discussion of behavior of substrings
    ;; holding onto references of the original strings, which can
    ;; significantly affect your memory usage in some cases.
Log in to add / edit an example.

See Also top

Log in to add a see also.

Plus_12x12 Minus_12x12 Source clojure/string.clj:169 top

(defn split
  "Splits string on a regular expression.  Optional argument limit is
  the maximum number of splits. Not lazy. Returns vector of the splits."
  {:added "1.2"}
  ([^CharSequence s ^Pattern re]
     (LazilyPersistentVector/createOwning (.split re s)))
  ([ ^CharSequence s ^Pattern re limit]
     (LazilyPersistentVector/createOwning (.split re s limit))))
Vars in clojure.string/split: defn
Used in 0 other vars

Comments top

6 comment(s) for split.

user=> (clojure.string/split "foo bar") java.lang.ClassNotFoundException: clojure.string

Do I need to require / use anything?

You need to add a namespace: user => (use 'clojure.string)

I 've a ruby if I've this:


I can do this


and get ("2", "9", "3", "8" .....)

How can I do this with clojure?...thanks

Hi, cocoOS. You may want to do it like this:

user=> (use '[clojure.string :only (split)])
user=> (def bignumber "2938434")
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")
user=> (seq "2938434")
(\2 \9 \3 \8 \4 \3 \4)

might also be what you want.

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

Log in to add a comment.