Add clojure.string functions for portability to ClojureScript


  • Type: Enhancement Enhancement
  • Status: Closed Closed
  • Priority: Major Major
  • Resolution: Completed
  • Affects Version/s: Release 1.6
  • Fix Version/s: Release 1.8
  • Component/s: None
  • Labels:
  • Patch:
    Code and Test
  • Approval:


It would be useful if a few common functions from Java's String were available as Clojure functions for the purposes of increasing portability to other Clojure platforms like ClojureScript.

The functions below also cover the vast majority of cases where Clojure users currently drop into Java interop for String calls - this tends to be an issue for discoverability and learning. While the goal of this ticket is increased portability, improving that is a nice secondary benefit.

Proposed clojure.string fn java.lang.String method
index-of indexOf
last-index-of lastIndexOf
starts-with? startsWith
ends-with? endsWith
includes? contains


  • clj-1449-7.patch - uses nil to indicate not-found in index-of

Performance: Tested the following with criterium for execution mean time (all times in ns).

Java Clojure Java Clojure (-4 patch) Clojure (-6 patch)
(.indexOf "banana" "n") (clojure.string/index-of "banana" "n") 8.70 9.03 9.27
(.indexOf "banana" "n" 1) (clojure.string/index-of "banana" "n" 1) 7.29 7.61 7.66
(.indexOf "banana" (int \n)) (clojure.string/index-of "banana" \n) 5.34 6.20 6.20
(.indexOf "banana" (int \n) 1) (clojure.string/index-of "banana" \n 1) 5.38 6.19 6.24
(.startsWith "apple" "a") (clojure.string/starts-with? "apple" "a") 4.84 5.71 5.65
(.endsWith "apple" "e") (clojure.string/ends-with? "apple" "e") 4.82 5.68 5.70
(.contains "baked apple pie" "apple") (clojure.string/includes? "baked apple pie" "apple") 10.78 11.99 12.17

Screened by: Stu, who prefers nil over -1 - add_functions_to_strings-6.patch

Note: In both Java and JavaScript, indexOf will return -1 to indicate not-found, so this is (at least in these two cases) the status quo. The benefit of nil return in Clojure is that it can be used as a found/not-found condition. The benefit of a -1 return is that it matches Java/JavaScript and that the return type can be hinted as a primitive long, allowing you to feed it into some other arithmetic or string operation without boxing.

  1. add_functions_to_strings.patch
    08/Jul/15 12:46 PM
    3 kB
    Nola Stowe
  2. add_functions_to_strings-2.patch
    14/Jul/15 8:55 PM
    4 kB
    Nola Stowe
  3. add_functions_to_strings-3.patch
    14/Jul/15 8:56 PM
    5 kB
    Nola Stowe
  4. add_functions_to_strings-4.patch
    14/Jul/15 9:56 PM
    5 kB
    Alex Miller
  5. add_functions_to_strings-5.patch
    26/Jul/15 9:28 AM
    5 kB
    Nola Stowe
  6. add_functions_to_strings-6.patch
    26/Jul/15 9:32 AM
    5 kB
    Alex Miller
  7. add_functions_to_strings-7.patch
    24/Aug/15 11:56 AM
    5 kB
    Nola Stowe
  8. clj-1449-more-v1.patch
    30/Aug/14 3:40 PM
    4 kB
    Andy Fingerhut



Vote (29)
Watch (5)


  • Created: