Skip to content
Related Articles
Open in App
Not now

Related Articles

Scala String indexOf() method with example

Improve Article
Save Article
  • Last Updated : 03 Oct, 2019
Improve Article
Save Article

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

My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!