Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

5
  • That was quite helpful. Speaking as a python programmer, I am using that to replace the "in" keyword (which may or may not be unorthodox, I am unsure) but it works for more than just a single character. Commented Dec 29, 2014 at 5:43
  • 1
    I don't understand how this was voted at the answer. I can see that a lot of people come here by Googling. A better way would definitely be using a regular expression. Commented Oct 13, 2015 at 18:32
  • 41
    You would use a regular expression to check for a single character? That's an excessive amount of overhead to get the exact same thing the built in function does. There are a lot of people who don't understand regex, and generally the simpler answer is the best. Commented Oct 13, 2015 at 19:19
  • I would go with /hello/g.test(your_string). While indexOf works, I think a regex test tells a better story of what you're trying to accomplish. If I'm trying to find a sequence of characters inside a string, the index is irrelevant. Commented Feb 22, 2017 at 21:49
  • Docs for indexOf() Commented May 24, 2018 at 13:14