ClojureScript substring index

There seems to be no built-in CLJS method to check the index of a substring (for example, the index of "scr" in "clojurescript" is 7). This can be done using regular expressions as described in this question , but it is quite verbose and a bit overkill for general use. Is there a way to quickly and easily check for the presence of a character or substring within a string?

+3


source to share


1 answer


Since ClojureScript has access to all native JavaScript, we can use built-in JS functions such as . indexOf . This makes for a pretty straightforward way to do things like:

> (.indexOf "clojurescript" "scr")
  7

      



As Joaquin pointed out, it is also very easy to detect the existence of a substring:

> (not= -1 (.indexOf "clojurescript" "scr"))
  true
> (not= -1 (.indexOf "clojurescript" "asd"))
  false

      

+8


source







All Articles