150

How can I check if a string contains another string instead of using "==" to compare the whole string?

Regards

1
  • Here a benchmark for the most common ways to check if a string is in a string: jsben.ch/#/o6KmH Commented Oct 31, 2016 at 17:05

1 Answer 1

214

You can use .indexOf():

if(str.indexOf(substr) > -1) {

}
Sign up to request clarification or add additional context in comments.

7 Comments

didn't work when I use msgCache[id].headers.subject.indexOf("DELIVERY FAILURE:") > -1
and the subject is: DELIVERY FAILURE: User fdfd.ablfdfitt ([email protected]) not listed in Domino Directory
Well, what is msgCache[id].headers.subject?
@donald: Works for me: jsfiddle.net/bZUvp (and it also works with node.js, it's the same JavaScript)
Actually, the message is inside an array [ 'DELIVERY FAILURE: User fdfd.ablfdfitt ([email protected]) not listed in Domino Directory' ] sorry for the mistake.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.