Testing for a specific set of characters with in a string

C

CES

All,
Sorry for the 101 question but I was wondering if their was an easer way to test for a specific substring within a string .

I know I could split the string into an array ie:

var str = “this is a string”;

str.split(" is a ");

And then test if the array[1] that is produced is null but I was just wondering if their was a easier way of doing this. Thanks in advance - CES
 
K

Kevin Newman

var str = "this is a string";

if (str.indexOf(" is a ") != -1) {
// do stuff
}

// or (4.0+ browsers)

if (/ is a /.test(str)) {
// do stuff
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top