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:
object GfG
{
def main(args : Array[String])
{
val result = "GeeksforGeeks" .lastIndexOf( 'e' , 4 )
println(result)
}
}
|
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:
object GfG
{
def main(args : Array[String])
{
val result = "GeeksforGeeks" .lastIndexOf( 'f' , 1 )
println(result)
}
}
|
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