Learnerslesson
   JAVA   
  SPRING  
  SPRINGBOOT  
 HIBERNATE 
  HADOOP  
   HIVE   
   ALGORITHMS   
   PYTHON   
   GO   
   KOTLIN   
   C#   
   RUBY   
   C++   
   HTML   
   CSS   
   JAVA SCRIPT   
   JQUERY   




lastIndexOf() - FUNCTION


lastIndexOf() Function


The lastIndexOf() Function is used to find the position of the last occurrence of the substring.


Example :



<html>
<body>  
<script>

	var x = "The Beautiful world is Beautiful indeed"
	var y = x.lastIndexOf("Beautiful")
	document.write("The substring is located at the position ",y)
    
</script>      
</body>
</html>


Output :



  The substring is located at the position 23

In the above code, we have declared a String Hello Beautiful World and assigned it to a variable x.


var x = "Hello Beautiful World"
java_Collections


And we would be searching the second occurence of the substring Beautiful, from the String, The Beautiful world is Beautiful indeed.


So, we have used the lastIndexOf() Function to find the substring Beautiful.


var y = x.lastIndexOf("Beautiful")

And what lastIndexOf() Function does is, goes to the String The Beautiful world is Beautiful indeed and searches for the position of the last occurence of the substring Beautiful.


And finds the substring Beautiful in the 23trd position. And the position (i.e. 23) is stored in variable y.

java_Collections

And thus prints the position.


document.write("The substring is located at the position ",y)

Output :



  The substring is located at the position 23