There are a couple of ways in which
we can find if a string contains
another string or substring.
The indexOf()
method helps us
locate a substring inside a string.
The method accepts two arguments -
substring
and
fromIndex.
The default value of fromIndex
is 0
.
Let's take a look at an example,
where we pass only the substring.
In the example given above,
the first occurrence of "es",
inside "Sam loves mangoes."
is at the index 7
and
hence the output is 7
.
Now let's take a look at an example,
where we pass both the substring
and
fromIndex arguments.
In the example given above,
the method looks for the substring
from the index 9
.
In other words,
it looks for the substring
inside " mangoes."
In this case,
the first occurrence of "es"
is at the index 15
.
If there are no matches,
the method would return -1
.
In the above example,
there were no occurrences of "xy"
inside "Sam loves mangoes."
and so we go the output as -1
.