String.indexOf()

This method returns the index within the calling String object of the first occurrence of the specified value, starting the search at fromIndex or -1 if the value is not found.

Syntax:

string.indexOf(searchValue[, fromIndex])

Argument Details

searchValue − A string representing the value to search for.

fromIndex − The location within the calling string to start the search from. It can be any integer between 0 and the length of the string. The default value is 0.

EXAMPLE ❯


String.lastIndexOf()

The lastIndexOf() method returns the position of the last occurrence of a specified value in a string.

lastIndexOf() searches the string from the end to the beginning, but returns the index s from the beginning, starting at position 0.

lastIndexOf() returns -1 if the value is not found.

lastIndexOf() is case sensitive.

Syntax:

string.lastIndexOf(searchvalue, start)

Argument Details

searchvalue - Required. The string to search for

start - Optional. The position where to start the search (searching backwards). If omitted, the default value is the length of the string

EXAMPLE ❯


String.startsWith()

The str.startsWith() method is used to check whether the given string starts with the characters of the specified string or not. 

Syntax: 

str.startsWith( searchString , position )

Argument Details

searchString: It is required parameter. It stores the string which needs to search.

start: It determines the position in the given string from where the searchString is to be searched. The default value is zero.

EXAMPLE ❯


String.endsWith()

The endsWith() method returns true if a string ends with a specified value, otherwise false:

Syntax: 

str.endsWith(searchString, length)

Argument Details

The first argument to this function is a string of characters searchString which is to be searched at the end of the given string. The second argument to the function is length which determines the length of the given string from the beginning to be searched for the searchString.

Note: The endsWith() method is case sensitive.

EXAMPLE ❯


String.search()

The saearch() method searches a string for a specified value and returns the position of the match:

EXAMPLE ❯


Did You Notice?

The two methods, indexOf() and search(), are equal?

The two methods are NOT equal. These are the differences:

  • The search() method cannot take a second start position argument.
  • The indexOf() method cannot take powerful search values (regular expressions).

String.match()

The match() method searches a string for a match against a regular expression, and returns the matches, as an Array object.

Syntax: 

string.match(regexp)

Argument Details

regexp  - Required. The value to search for, as a regular expression.

Returns - An Array, containing the matches, one item for each match, or null if no match is found

EXAMPLE ❯


String.includes()

The includes() method returns true if a string contains a specified value.

Returns true if the string contains the value, otherwise false.

Syntax: 

string.includes(searchvalue, start)

Argument Details

searchvalue - Required. The string to search for

start - Optional. Default 0. Position to start the search

EXAMPLE ❯