Join the Stack Overflow Community
Stack Overflow is a community of 6.4 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

Usually, I would expect a String.contains() method, but there doesn't seem to be one. What is a reasonable way to check for this?

share|improve this question
13  
It's easy with the indexOf method, you can see a tutorial of indexOf and substring here: dreamsyssoft.com/javascript-shell-scripting/… – Triton Man Mar 30 '15 at 17:53
5  
possible duplicate of JQuery string contains check – Saswat Aug 18 '15 at 11:14
5  
you can see speed of r.indexOf(s) !== -1; fastest than others. hayageek.com/javascript-string-contains – Sherali Turdiyev Oct 1 '15 at 5:37
    
you can try this "foo bar".match('oo') ? console.log("contained") : console.log("not contained"); – Alejandro Vales May 24 at 10:51
2  
Here a benchmark for the most common ways to check if a string is in a string: jsben.ch/#/o6KmH – EscapeNetscape Oct 26 at 9:38

40 Answers 40

If you don't like the !!~, etc. tricks, you can simply add +1 to the result of .indexOf(). This way if a string is not found, -1 + 1 = 0 will be falsy, 0.. + 1 = 1.. will be truthy:

if ("StackOverflow".indexOf("Stack") + 1 )
    alert('contains');
else 
    alert('does not contain');
share|improve this answer
3  
Unreadable for human being... – jmcollin92 Oct 5 '14 at 16:34
1  
I perfectly agree, and I don't use it myself, but readability wasn't in the question :) – biziclop Oct 6 '14 at 9:11

This is a function to check if a substring is existing in a string or not:

function isStringMatch(str, str_to_match) {
    return (str.indexOf(str_to_match) > -1);
}
share|improve this answer
3  
Just a heads up... this should be either >=0, >-1, or (my preference) !== -1, as this will fail in its current form if the string begins with the substring. – Karl White Feb 1 '14 at 8:07

I know that best way is str.indexOf(s) !== -1; http://hayageek.com/javascript-string-contains/

I suggest another way(str.replace(s1, "") !== str):

var str = "Hello World!", s1 = "ello", s2 = "elloo";
alert(str.replace(s1, "") !== str);
alert(str.replace(s2, "") !== str);

share|improve this answer

Try this:

if ('Hello, World!'.indexOf('orl') !== -1)
    alert("The string 'Hello World' contains the substring 'orl'!");
else
    alert("The string 'Hello World' does not contain the substring 'orl'!");

Here is an example: jsfiddle

share|improve this answer

You can use indexOf which returns the position of the string. If not found, it will return -1. So if the method returns -1 string doesn't exist

var string = "This is a test string",
substring = "test";
if(string.indexOf(substring) >= 0)
  //substring exist
else
  //substring does not exist
share|improve this answer

You can also do something like this

var snipers = " Vasily Zaytsev, Simo Hayha, Chris Kyle";
var me = "Josip";

function printSniperStatus (person) {
    if (aContainsB(snipers, person)) {
        console.log(person + " is a sniper.");
    } else {
        console.log(person + " is NOT a sniper.");
    }
}

// Outputs: "Josip is NOT a sniper."
printSniperStatus(me);
share|improve this answer
    
Is aContainsB a standard javascript function? I found it to be a custom function here: adripofjavascript.com/blog/drips/…. The function itself is: function aContainsB (a, b) { return a.indexOf(b) >= 0; }. – Dov Miller Sep 15 at 12:24

With ECMAScript 2015, we can use includes()

let s = "foo";
console.log(s.includes("oo"));
share|improve this answer
    
This is just repeating some of the existing answers. – Pang Oct 8 at 3:55

One more function, search:

var str = "Stack Overflow";
var n = str.search("Overflow");
if (n != -1)
    alert('String exists')
share|improve this answer
1  
search is for regular expressions – Gothdo Dec 11 '15 at 18:30

There are multiple ways to do this. But most of the time, you will be fine using the indexOf() method. indexOf() returns the position of the string passed to it as argument and -1 if the string on which it was called doesn't contain the argument string.

var str = "A cat and a dog";
str.indexOf("cat"); // returns 2
str.indexOf("panda"); // returns -1
share|improve this answer
result = 'GBP|1800';
//if pipe delimeter is there it returns true else false.
if(result.indexOf("|"))
{
    console.log('go default:' +result);
    var cur = result.substring(0, 3);//returns GBP
    console.log('go default cur:' +cur);
    var minmum_fee = result.substring(4);//gets the substring amount
    console.log('go default minmum_fee:' +minmum_fee);

}
else
{
    console.log('not found:' +result);
}
share|improve this answer

protected by Sean Vieira Nov 2 '12 at 12:31

Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).

Would you like to answer one of these unanswered questions instead?

Not the answer you're looking for? Browse other questions tagged or ask your own question.