Open In App

Scala String indexOf() method with example

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

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:




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


Output:

1

So, here the first occurrence of ‘i’ is at index one So, 1 is returned as output.
Example #2:




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


Output:

3


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads