Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Index of a substring in ClojureScript

There doesn't seem to be a built-in CLJS method to check for the index of a substring (e.g. the index of "scr" in "clojurescript" is 7). This can be done with regexes as described in this question, but that's quite verbose and is a little bit overkill for common use. Is there any way to quickly and easily check for the existence of a character or substring within a string?

like image 879
Chase Sandmann Avatar asked Jan 10 '23 06:01

Chase Sandmann


1 Answers

Because ClojureScript has access to all native JavaScript, we can use built-in JS functions like .indexOf. This makes it fairly simple to do things like:

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

As Joaquin noted, this also makes it very simple to determine the existence of a substring as well:

> (not= -1 (.indexOf "clojurescript" "scr"))
  true
> (not= -1 (.indexOf "clojurescript" "asd"))
  false
like image 111
Chase Sandmann Avatar answered Jan 12 '23 04:01

Chase Sandmann