You're viewing version 1.2.0 of subs. The latest stable version of Clojure Core is 1.3.0.
1.2.0 Arrow_down_16x16
  • (subs s start)
  • (subs s start end)
Returns the substring of s beginning at start inclusive, and ending
at end (defaults to length of string), exclusive.

2 Examples top

  • user=> (subs "Clojure" 1)    
    user=> (subs "Clojure" 1 3)
    ;; String indexes have to be between 0 and (.length s)
    user=> (subs "Clojure" 1 20)
    java.lang.StringIndexOutOfBoundsException: String index out of range: 20 (NO_SOURCE_FILE:0)
  • ;; Note that subs uses method java.lang.String/substring
    ;; See this link for more details:
    ;; Briefly, before Java version 7u6, Java's substring method was
    ;; guaranteed to work in O(1) time, by creating a String that refers
    ;; to the original string's characters, rather than copying them.
    ;; After Java 7u6, substring was changed to copy the string's original
    ;; characters, thus taking time linear in the length of the substring,
    ;; but it never refers to the original string.
    ;; The potential disadvantage of the pre-version-7u6 behavior is that
    ;; if you read in one or more strings with a large total size, and then
    ;; use methods based on substring to keep only a small subset of that,
    ;; e.g., because you parsed and found a small collection of substrings of
    ;; interest for your computation, the large strings will still have
    ;; references to them, and thus cannot be garbage collected, even if you
    ;; have no other references to them.
    ;; You can use the Java constructor (String. s) to guarantee that you
    ;; copy a string s.  (String. (subs s 5 20)) will copy the substring once
    ;; pre-version-7u6, but it will copy the substring twice
    ;; post-version-7u6.
    ;; I believe java.util.regex.Matcher method group, and
    ;; java.util.regex.Pattern method split, also have the same
    ;; behavior as substring.
    ;; This affects the behavior of Clojure functions such as:
    ;; In clojure.core:
    ;; subs, re-find, re-matches, re-seq
    ;; In clojure.string:
    ;; replace replace-first split
Log in to add / edit an example.

See Also top

Log in to add a see also.

Plus_12x12 Minus_12x12 Source clojure/core.clj:3940 top

(defn ^String subs
  "Returns the substring of s beginning at start inclusive, and ending
  at end (defaults to length of string), exclusive."
  {:added "1.0"}
  ([^String s start] (. s (substring start)))
  ([^String s start end] (. s (substring start end))))
Vars in clojure.core/subs: defn

Comments top

No comments for subs. Log in to add a comment.