ClojureDocs

Nav

Namespaces

subs

clojure.core

Available since 1.0 (source)
  • (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.
3 Examples
user=> (subs "Clojure" 1)    
"lojure"
user=> (subs "Clojure" 1 3)
"lo"


;; 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
;; http://docs.oracle.com/javase/6/docs/api/java/lang/String.html#substring%28int,%20int%29

;; See this link for more details:

;; https://dzone.com/articles/changes-stringsubstring-java-7

;; This link was the original, but seems to no longer exist as of Nov 2017:
;; http://www.javaadvent.com/2012/12/changes-to-stringsubstring-in-java-7.html

;; 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
;; Suppose you want to shorten a string of blanks [outdent]
;; Starting with a string of length 5
;; [Here I do not use blanks to illustrate what is happening]
(def s5 "abcdef")
;; and reducing its length by 2
(subs s5 (min 2 (count s5))) 
;=> "cdef"
(subs s5 (min 10 (count s5))) 
;=> ""
See Also

Replaces all instance of match with replacement in s. match/replacement can be: string / str...

Added by mmwaikar

Splits string on a regular expression. Optional argument limit is the maximum number of parts. No...

Added by mmwaikar

Replaces the first instance of match with replacement in s. match/replacement can be: char /...

Added by jafingerhut

Returns the next regex match, if any, of string to pattern, using java.util.regex.Matcher.find(). ...

Added by jafingerhut

Returns a lazy sequence of successive matches of pattern in string, using java.util.regex.Matcher....

Added by jafingerhut

Returns the match, if any, of string to pattern, using java.util.regex.Matcher.matches(). Uses re...

Added by jafingerhut
0 Notes
No notes for subs