JavaScript string indexof() method with example

In this post, we are going to explore the JavaScript string indexof() method with the help of examples. Sometimes we have to perform an operation and need to search for some sub-string in the main string, finding index and all occurrence of a sub-string and case-insensitive match. We can achieve all this using the JavaScript string indexof() method which we are going to understand in this post with examples.

What is JavaScript string indexof() method?


The JavaScript string indexof() is a built-in method that returns the index of the first occurrence of a specified sub-string value in a string. It returns -1 if a specified sub-string value is not found in a string.

Syntax for string indexof() method

string.indexof(searchValue,startindex)

Parameters of string indexof()

  • search value: It is the required parameter that represents the sub-string that we need to search for.
  • startindex : It is an optional parameter that represents the index at which the search will start. It’s default value is 0 ,means if we do not pass startindex value then the search will start from 0.

Returns Value

It returns -1 if a specified sub-string value is not found in a string.

Note: The string indexof() method executes a case-insensitive search.

Example of JavaScript string indexof()


1. indexof() to Find all indexes of letter in String

In this example, we are finding all indexes of the letter ‘e’ in a string using the JavaScript string method indexof().

//Js code to  Find all occurrence of letter in String

function findindex(char,string)
{
  var array =[],count =-1;
  while((count=string.indexOf(char,count+1)) >= 0) 
  array.push(count);
  return array;
}


console.log('All indexes of e char :',findindex('e',"devenum"));

The Output will be:

All indexes of e char :[ 1, 3 ]

2.Indexof() to find count of substring in a string

In this example, we are using the JavaScript string indexof() method to find a count of a substring(is) in mystring. If the substring match in mystring , then on each match we increase the occurrence count to find out the number of times a given substring exists in mystring.

//JS code to find count of  substring in a string


let mystring = 'This is JavaScript example.This is devenum.com ';
let substring = 'is';

let occurrence = 0;

let position = mystring.indexOf(substring);

while(position !== -1) 
{
    occurrence++;
    position = mystring.indexOf(substring, position + 1);
}

console.log('count of is in mystring: ',occurrence);

The Output will be:

count of is in mystring:  4

3.Find index of substring in String Using indexof()

In this example, we are using the JavaScript string indexof() method to find a substring from mystring. If the substring match in mystring, then indexof() will return the index of substring else it will return -1.

//JS code to Find index of substring in String

let mystring = 'This is JavaScript example.This is devenum.com ';
let index = mystring.indexOf('devenum')


console.log('index of substring: ',index);

the Output will be:

index of substring:  35

4. Javascript string indexof() with case insesentive

JavaScript string indexof() is a case-insensitive method. Let us understand with an example.

We are looking for ”devenum” in mystring it is returning -1. Because the mystring contains uppercase “DEVENUM”, not “devenum“.

//Js Program to match case insesentive of substring in String

let mystring = 'The DEVENUM is ';
let substring = 'devenum'
let index = mystring.indexOf(substring)
console.log('index of substring: ',index);

The Output will be:

index of substring:  -1

To solve this we can convert both mystring and substring into lowercase by using function toLocaleLowerCase() and can search for index.

As we can see it returns the index of substring found in mystring

let mystring = 'The DEVENUM is ';
let substring = 'devenum'


let index =  mystring.toLocaleLowerCase().indexOf(substring.toLocaleLowerCase());
console.log('index of substring: ',index);

The Output will be:

index of substring:  4

Summary

In this post, we have explored the JavaScript string indexof() method with examples that includes Indexof() to find substring in a string, How to use Javascript string indexof() with case insensitive, Indexof() to find the count of a substring in a string.