String contains String JavaScript

This is usefull to find a string inside another in JavaScrit.

this function accepts two params, first the string to search and the second the all string;

example:


var str = 'That looks nice doesn it';

var val = 'nice';

var res = contains(val, str);

console.log(res);

function contains(ogStr, newStr) {
var ogStrLen = ogStr.length;
var index = newStr.indexOf(ogStr);

if(newStr.substr(index, ogStrLen + 1).trim() === ogStr) {
return true;
}

return false;
}

 

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s