How to check a string “StartsWith” another String and one string contains another substring? in Javascript


In JavaScript ,
var carname = 'Volvo XC60'; String indexes are zero-based: The first character is in position 0, the second in 1, and so on.

How to check a string “StartsWith” another String

You can use ECMAScript 6's String.prototype.startsWith() method, but it's not yet supported in all browsers.

you can use it like this:

"Hello World!".startsWith("He"); // true

var haystack = "Hello world";
var prefix = 'orl';
haystack.startsWith(prefix); // false

Another alternative with .lastIndexOf:

haystack.lastIndexOf(needle, 0) === 0


How to check one string contains another substring

String.prototype.indexOf returns the position of the string in the other string. If not found, it will return -1.

var string = "foo",
    substring = "oo";
console.log(string.indexOf(substring) > -1);


Or some times it works with,javaScript is case sensitive.

indexof() should actually be indexOf()

Try fixing it and see if that helps:

if (test.indexOf("title") !=-1) {
    alert(elm);
    foundLinks++;
}


2 comments: