Clojure

Add starts-with? ends-with? contains? to clojure.string

Details

  • Type: Enhancement Enhancement
  • Status: Open Open
  • Priority: Minor Minor
  • Resolution: Unresolved
  • Affects Version/s: Release 1.6
  • Fix Version/s: None
  • Component/s: None
  • Labels:
  • Patch:
    Code
  • Approval:
    Triaged

Description

Add clojure.string/starts-with? ends-with? and contains?, similar to java.lang.String's startsWith/endsWith/contains. In addition to making these easier to find and use, this provides a place to add a portable ClojureScript variant.

Activity

Hide
Alex Miller added a comment -

Re substring, there is a clojure.core/subs for this (predates the string ns I believe).

clojure.core/subs
([s start] [s start end])
Returns the substring of s beginning at start inclusive, and ending
at end (defaults to length of string), exclusive.

Show
Alex Miller added a comment - Re substring, there is a clojure.core/subs for this (predates the string ns I believe). clojure.core/subs ([s start] [s start end]) Returns the substring of s beginning at start inclusive, and ending at end (defaults to length of string), exclusive.
Alex Miller made changes -
Field Original Value New Value
Approval Triaged [ 10120 ]
Labels string
Alex Miller made changes -
Description While `clojure.string` wraps most of `java.lang.String`'s method, for some reason `startsWith` and `endsWith` were left out. Sure, in Java, one can use them fairly easy via the interop, but what if you're using ClojureScript (JavaScript's String class has not methods)? There's also the question of inconsistency, as the criteria by which String method were chosen for wrapping is unclear.

Should you approve this for inclusion I'd happily implement it.

P.S. The omission of `substring` is also a bit puzzling.
Add clojure.string/starts-with? and ends-with? similar to java.lang.String's startsWith/endsWith. In addition to making these easier to find and use, this provides a place to add a portable ClojureScript variant.
Hide
Jozef Wagner added a comment -

As strings are collection of characters, you can use Clojure's sequence facilities to achieve such functionality:

user=> (= (first "asdf") \a)
true
user=> (= (last "asdf") \a)
false
Show
Jozef Wagner added a comment - As strings are collection of characters, you can use Clojure's sequence facilities to achieve such functionality:
user=> (= (first "asdf") \a)
true
user=> (= (last "asdf") \a)
false
Hide
Alex Miller added a comment -

Jozef, String.startsWith() checks for a prefix string, not just a prefix char.

Show
Alex Miller added a comment - Jozef, String.startsWith() checks for a prefix string, not just a prefix char.
Hide
Bozhidar Batsov added a comment -

Re substring, I know about subs, but it seems very odd that it's not in the string ns. After all most people will likely look for string-related functionality in clojure.string. I think it'd be best if `subs` was added to clojure.string and clojure.core/subs was deprecated.

Show
Bozhidar Batsov added a comment - Re substring, I know about subs, but it seems very odd that it's not in the string ns. After all most people will likely look for string-related functionality in clojure.string. I think it'd be best if `subs` was added to clojure.string and clojure.core/subs was deprecated.
Hide
Pierre Masci added a comment -

Hi, I was thinking the same about starts-with and .ends-with, as well as (.indexOf s "c") and (.lastIndexOf "c").

I read the whole Java String API recently, and these 4 functions seem to be the only ones that don't have an equivalent in Clojure.
It would be nice to have them.

Andy Fingerhut who maintains the Clojure Cheatsheet told me: "I maintain the cheatsheet, and I put .indexOf and .lastIndexOf on there since they are probably the most common thing I saw asked about that is in the Java API but not the Clojure API, for strings."
Which shows that there is a demand.

Because Clojure is being hosted on several platforms, and might be hosted on more in the future, I think these functions should be part of the de-facto ecosystem.

Show
Pierre Masci added a comment - Hi, I was thinking the same about starts-with and .ends-with, as well as (.indexOf s "c") and (.lastIndexOf "c"). I read the whole Java String API recently, and these 4 functions seem to be the only ones that don't have an equivalent in Clojure. It would be nice to have them. Andy Fingerhut who maintains the Clojure Cheatsheet told me: "I maintain the cheatsheet, and I put .indexOf and .lastIndexOf on there since they are probably the most common thing I saw asked about that is in the Java API but not the Clojure API, for strings." Which shows that there is a demand. Because Clojure is being hosted on several platforms, and might be hosted on more in the future, I think these functions should be part of the de-facto ecosystem.
Hide
Andy Fingerhut added a comment -

Updating summary line and description to add contains? as well. I can back this off if it changes your mind about triaging it, Alex.

Show
Andy Fingerhut added a comment - Updating summary line and description to add contains? as well. I can back this off if it changes your mind about triaging it, Alex.
Andy Fingerhut made changes -
Description Add clojure.string/starts-with? and ends-with? similar to java.lang.String's startsWith/endsWith. In addition to making these easier to find and use, this provides a place to add a portable ClojureScript variant.
Add clojure.string/starts-with? ends-with? and contains?, similar to java.lang.String's startsWith/endsWith/contains. In addition to making these easier to find and use, this provides a place to add a portable ClojureScript variant.
Summary Add starts-with? and ends-with? to clojure.string Add starts-with? ends-with? contains? to clojure.string
Hide
Andy Fingerhut added a comment -

Patch clj-1449-basic-v1.patch dated Aug 30 2014 adds starts-with? ends-with? contains? functions to clojure.string.

Patch clj-1449-more-v1.patch is the same, except it also replaces several Java method calls with calls to these Clojure functions.

Show
Andy Fingerhut added a comment - Patch clj-1449-basic-v1.patch dated Aug 30 2014 adds starts-with? ends-with? contains? functions to clojure.string. Patch clj-1449-more-v1.patch is the same, except it also replaces several Java method calls with calls to these Clojure functions.
Andy Fingerhut made changes -
Attachment clj-1149-basic-v1.patch [ 13294 ]
Andy Fingerhut made changes -
Attachment clj-1449-more-v1.patch [ 13295 ]
Hide
Andy Fingerhut added a comment -

Patch clj-1449-basic-v1.patch dated Sep 5 2014 is identical to the patch I added recently called clj-1149-basic-v1.patch. It is simply renamed without the typo'd ticket number in the file name.

Show
Andy Fingerhut added a comment - Patch clj-1449-basic-v1.patch dated Sep 5 2014 is identical to the patch I added recently called clj-1149-basic-v1.patch. It is simply renamed without the typo'd ticket number in the file name.
Andy Fingerhut made changes -
Attachment clj-1449-basic-v1.patch [ 13316 ]
Andy Fingerhut made changes -
Attachment clj-1149-basic-v1.patch [ 13294 ]
Andy Fingerhut made changes -
Patch Code [ 10001 ]

People

Vote (11)
Watch (1)

Dates

  • Created:
    Updated: