Scala String lastIndexOf(int ch, int fromIndex)() method with example
Last Updated :
03 Oct, 2019
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.
Share your thoughts in the comments
Please Login to comment...