JavaScript string contains

This is a simple function in JavaScript, I use it to check if a string contains another string.The different in this function is that the return value is the position+1.So you could use it in an If without to check the return value manually, just see my test from 1 to 3.

This is the function:

function string_contains(str,needle) {

Now we make a little test to see if it work:

//Test 1
console.log("abcdef contains a");
console.log("abcdef not contains a");
//Test 2
console.log("abcdef contains b");
console.log("abcdef not contains b");
//Test 3
console.log("abcdef contains z");
console.log("abcdef not contains z");

Seems good:

abcdef contains a
abcdef contains b
abcdef not contains z

3 thoughts on “JavaScript string contains”

  1. whoah this weblog is magnificent i really like reading your
    articles. Stay up the good work! You already know,
    lots of persons are searching round for this information, you could aid them greatly.

  2. Wow, this post is fastidious, my sister is analyzing these
    kinds of things, therefore I am going to inform her.

Leave a Reply

Your email address will not be published.

one × 1 =