views:

61

answers:

3

Hello,

I need to check a string to see if it contains another string.

var str1 = "ABCDEFGHIJKLMNOP";
var str2 = "DEFG";

What function do I use to find out if str1 contains str2?

Cheers, Thomas.

+3  A: 

You can use javascript's indexOf function.

var str1 = "ABCDEFGHIJKLMNOP";
var str2 = "DEFG";
if(str1.indexOf(str2) != -1){
    alert(str2 + " found");
}
Rocket
Ah yes that works, cheers.
diggersworld
You're welcome.
Rocket
+1  A: 

Check out JavaScript's indexOf function. http://www.w3schools.com/jsref/jsref_IndexOf.asp

Charlie boy
+2  A: 

In Javascript:

if (str1.indexOf(str2)!=-1) alert('Contains string!');
Rudu