indexOf & lastIndexOf to get location of a stringWe can find out the location of a particular string inside the main string by using indexOf function. This is some what opposite of the charAt function which returns the char once a location is given. By using indexOf function we can find out the location and this function returns a number. The first character of the main string is considered as 0 location. Here is an example of indexOf function.
The above code will return 15
lastIndexOfThis function returns the position of string by checking from end or right side of the string. This way we can find out the last occurrence of the string from the left but the location is given from right side.
Here is the code.
Output of above code is 16 as l position is counted from left side but this is the last occurrence of l
This article is written by plus2net.com team.