Skip to content

Latest commit

 

History

History
executable file
·
17 lines (13 loc) · 973 Bytes

File metadata and controls

executable file
·
17 lines (13 loc) · 973 Bytes

There are many methods that can be utilized when searching a string. Each method has different use case and most of them have different parameters. The methods are replace(), search(), indexOf(), and lastIndexOf(). The syntax and the functions of the methods are shown below.

string.replace(str1, str2)

  • replaces first occurrence of str1 with str2

string.search(regex)

  • searches for a substring based on regular expression (you will learn about this later)

string.indexOf(substring,position)

  • returns the left-most occurrence of substring in a string after the index position
  • position is optional and has default value of 0
  • if string doesn't contain substring, returns -1

string.lastIndexOf(substring,position)

  • returns the right-most occurrence of substring in a string before the index position
  • position is optional, the default value is string.length
  • if string doesn't contain substring, returns -1