The Wayback Machine - https://web.archive.org/web/20220929092308/https://www.geeksforgeeks.org/scala-string-indexofstring-str-method-with-example/
Skip to content
Related Articles

Related Articles

Scala String indexOf(String str) method with example

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

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

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!