Open In App

Scala String lastIndexOf(int ch, int fromIndex)() method with example

Last Updated : 03 Oct, 2019
Improve
Improve
Like Article
Like
Save
Share
Report

The lastIndexOf() method is utilized to return the index of the last appearance of the character we specify in the argument from the index we specify for the stated string.

Method Definition: int lastIndexOf(int ch, int fromIndex)
Return Type: It returns the index of the last appearance of the character in the argument from the index we specify.

Example #1:




// Scala program of int lastIndexOf()
// method
  
// Creating object
object GfG
  
    // Main method
    def main(args:Array[String])
    {
      
        // Applying lastIndexOf method
        val result = "GeeksforGeeks".lastIndexOf('e', 4)
          
        // Displays output
        println(result)
      
    }


Output:

2

Here, the searching begins from the index 4, from right to the left position and the one which is appearing first is returned.
Example #2:




// Scala program of int lastIndexOf()
// method
  
// Creating object
object GfG
  
    // Main method
    def main(args:Array[String])
    {
      
        // Applying lastIndexOf method
        val result = "GeeksforGeeks".lastIndexOf('f', 1)
          
        // Displays output
        println(result)
      
    }


Output:

-1

Here, the method is not able to find the stated character while searching from left towards right from the index specified. So, it returns -1 here.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads