JavaScript String Search

JavaScript String Search


JavaScript String Search
Therefore four Search Methods in JavaScript.
  • String indexOf()
  • String lastIndexOf()
  • String startsWith()
  • String endsWith()
JavaScript String indexOf()
In JavaScript, The indexOf() method returns the index of (the position of) the first occurrence of a specified text in a string.
Example
Output
Note
JavaScript counts positions from zero.
0 is the first position in a string, 1 is the second, 2 is the third, 3 is the fourth and so on.
JavaScript String lastIndexOf()
In JavaScript, The lastIndexOf() method returns the index of the last occurrence of a specified text in a string.
Example
Output
JavaScript String startsWith()
In JavaScript, The startsWith() method checks the index is starts with in method then returns true otherwise returns false.
Example
Output
JavaScript String search()
In JavaScript, The search() method searches a string for a specified value and returns the position of the match.
Example
Output