Open In App
Related Articles

Scala String indexOf(String str) method with example

Improve Article
Improve
Save Article
Save
Like Article
Like

The indexOf(String str) method is utilized to return the index of the sub-string which occurs first in the string stated.

Method Definition: indexOf(String str)
Return Type: It returns the index of the sub-string which is specified in the argument of the method.

Example #1:




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


Output:

2

Example #2:




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


Output:

5

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
Previous
Next
Similar Reads