4

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 1

9

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
Sign up to request clarification or add additional context in comments.

2 Comments

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"))
Good suggestion! That's actually what I was using it for in my code - I don't know how I forgot!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.