Open In App
Related Articles

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

Improve Article
Improve
Save Article
Save
Like Article
Like

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.


Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Last Updated : 03 Oct, 2019
Like Article
Save Article
Previous
Next
Similar Reads