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?
1 Answer
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
2 Comments
Joaquin
I would suggest you update the answer for the question: > Is there any way to quickly and easily check for the existence of a character or substring within a string?
(= -1 (.indexOf "clojurescript" "asd"))Chase Sandmann
Good suggestion! That's actually what I was using it for in my code - I don't know how I forgot!