indexOf – JavaScript check one string contains another string

The indexOf() method returns the index of the word or substring within the calling string if found, otherwise it returns the -1.
The indexOf() method was the case sensitive.
We can also start the searching in the string from the specific location.

Syntax:

string.indexOf("searchingString", [fromIndex]);

Here fromIndex is optional, by default value is 0. If you provide this value than it start the searching word from that specific location. If you provide its value > than string length than it directly returns the -1, without the searching in the string.

Examples:

var string = "JavaScript Hive";

alert(string.indexOf("Hive")); // returns 11
alert(string.indexOf("JavaScript")); // returns 0
alert(string.indexOf("e")); // returns 14
alert(string.indexOf("Hiveeee")); // returns -1
alert(string.indexOf("Hive", string.length + 1)); // returns -1
alert(string.indexOf("hive", string.length + 1)); // returns -1

Count total occurrences of the searching value in the string:

String.prototype.TotalOccurrences = function (searchingString) {
    var count = 0;
    var index = this.toString().indexOf(searchingString);
    while (index !== -1) {
        count++;
        index = this.toString().indexOf(searchingString, index + searchingString.length);
    }
    return count;
}
var string = "JavaScript Hive Hive Hive hive";
alert("Total occurrences of 'Hive' is: " + string.TotalOccurrences("Hive"));

Here we added the TotalOccurrences method to String class, now it is available to all the string. It returns the total number of occurrences of the searching string in the string.
The indexOf() method not returns the true or false, It always returns the index of the word in the string or -1. So its better to check the returns value of the indexOf() method like…

var string = "JavaScript Hive Hive Hive hive";
var index = string.indexOf("ABCD");
if (index !== -1) {
    alert("The substring found at the index: " + index);
} else {
    alert("No match found!!!");
}

JavaScript case-insensitive searching:

var string = "JavaScript Hive Hive Hive Hive";
var index = string.toLowerCase().indexOf("hive".toLowerCase());
if (index !== -1) {
    alert("The substring found at the index: " + index);
} else {
    alert("No match found!!!");
}

If you want to search the string wihout the care of the character case. Than simply first we can covert both string in same case either the lower case or capital case, after that we can use the same indexOf() method to check the string have the searching string.
If you only want to know that string have the searching string or not and also you want to check the string with case sensitive or case insensitive than the below code will help you to do that.

String.prototype.contains = function (searchingString, caseSensitive) {
    var string = this.toString();
    if (caseSensitive === undefined) {
        caseSensitive = true;
    }

    if (caseSensitive == true) {
        return string.indexOf(searchingString) !== -1;
    } else {
        return string.toLowerCase().indexOf(searchingString.toLowerCase()) !== -1;
    }
}

var string = "JavaScript Hive Hive Hive Hive";

if (string.contains("hive", true)) {
    alert("Match found!!! (Case sensitive)");
} else {
    alert("No match found!!! (Case sensitive)");
}

if (string.contains("hive", false)) {
    alert("Match found!!! (Case insensitive)");
} else {
    alert("No match found!!! (Case insensitive)");
}

In above code we are add the contains method to String class so now it is available to all the string. You can directly call that method by passing the searching string and the boolean value for the case sensitive.
If you pass the true than it will check the string with the case sensitive, or if you pass the false than it check the string case insensitive.
By default the value of the caseSensitive is true.

If you have any question please post below.


Happy Coding!!!

🙂