String Search Methods in JavaScript

01/04/2022

Contents

In this article, you will learn string search methods in JavaScript.

indexOf()

The indexOf () is a method to search the specified character string from the front in the character string.
As a result of the search, if the specified character string exists, the appearance position (index number) of the character string is returned, and if it does not exist, the value of “-1” is returned.

Below is a sample.

var str = 'JavaScript string search methods';
var search = 'string';

console.log(str.indexOf(search));

//result
//11

If you specify an integer as the second argument, you can specify the start position of the character string to be searched.

Let’s try specifying “12” as the second argument.

var str = 'JavaScript string search methods';
var search = 's';

console.log(str.indexOf(search, 12));
 
//result
//18

lastIndexOf()

The lastIndexOf() is a method to search the specified character string from the back in the character string.
As a result of the search, if the specified character string exists, the appearance position (index number) of the character string counted from the front is returned, and if it does not exist, the value of “-1” is returned.

Below is a sample.

var str = 'JavaScript string search methods';
var search = 's';

console.log(str.lastIndexOf(search));

//result
//31

As with the indexOf(), the second argument can be specified for the lastIndexOf().

Let’s try specifying “30” as the second argument.

var str = 'JavaScript string search methods';
var search = 's';

console.log(str.lastIndexOf(search, 30));

//result
//18

startsWith()

The startsWith() is a method to find out if a string starts with the specified string.
If the beginning of the string starts with the specified string, “true” is returned, otherwise “false” is returned.

Below is a sample.

var str = 'JavaScript string search methods';
var search = 'Ja';

console.log(str.startsWith(search));

//result
//true

You can specify the index number to start the search in the second argument to startsWith().

var str = 'JavaScript string search methods';
var search = 'st';

console.log(str.startsWith(search,11));

//result
//true

endsWith()

The endsWith() is a method to find out if a string ends with the specified string.
If the end of the string is the specified string, “true” is returned, and if it is not the specified string, “false” is returned.

Below is a sample.

var str = 'JavaScript string search methods';
var search = 'ds';

console.log(str.endsWith(search));

//result
//true

In the second argument, specify the length of the character string to be searched.

var str = 'JavaScript string search methods';
var search = 'ds';

console.log(str.endsWith(search,10));

//result
//false

includes()

The includes() is a method to search whether the specified string is included in the character string.
If the specified character string exists in the character string, “true” is returned, and if it does not exist, “false” is returned.

Below is a sample.

var str = 'JavaScript string search methods';
var search = 'str';

console.log(str.includes(search));

//result
//true

In the second argument, specify the index number to start the search.

var str = 'JavaScript string search methods';
var search = 'str';

console.log(str.includes(search, 12));

//result
//false