The indexOf() method is utilized to find the index of the first appearance of the character in the string and the character is present in the method as argument.
Method Definition: int indexOf(int ch)
Return Type: It returns the index of the character stated in the argument.
Example #1:
object GfG
{
def main(args : Array[String])
{
val result = "Nidhi" .indexOf( 'i' )
println(result)
}
}
|
So, here the first occurrence of ‘i’ is at index one So, 1 is returned as output.
Example #2:
object GfG
{
def main(args : Array[String])
{
val result = "Nidhi" .indexOf( 'h' )
println(result)
}
}
|