Is there a clojure counterpart for java.lang.String indexOf method?

949 views
Skip to first unread message

jingguo

unread,
Oct 9, 2011, 10:36:33 AM10/9/11
to Clojure
java.lang.String has a method called indexOf(String str). We can use
it with
clojure in the following way:

(.indexOf "012" "12")

Does clojure has a API for this? I have checked clojure.core and
clojure.string.
It seems that clojure does not have a counterpart for indexOf.

Stephen Compall

unread,
Oct 9, 2011, 11:47:56 AM10/9/11
to clo...@googlegroups.com
On Sun, 2011-10-09 at 07:36 -0700, jingguo wrote:
> Does clojure has a API for this? I have checked clojure.core and
> clojure.string.
> It seems that clojure does not have a counterpart for indexOf.

No, it doesn't.

There's usually a way to do string operations that you traditionally do
with indexOf using other tools like regular expressions; these also have
the benefit of helping you avoid fencepost errors.

For example, if you wanted all the text before and after "12" in a
string, you could match #"(?s)(.*?)12(.*)".

That aside, while the lack of a Clojure-level facility should give you a
nudge to consider such alternatives, it's perfectly fine to call Java
when needed.

--
Stephen Compall
^aCollection allSatisfy: [:each|aCondition]: less is better

jingguo

unread,
Oct 10, 2011, 9:49:34 AM10/10/11
to Clojure
Thanks for your clarification.
Reply all
Reply to author
Forward
0 new messages