How to Find a Substring of String in Scala

Mohammad Irfan Feb 02, 2024
  1. Use the contains() Function to Find Substring in Scala
  2. Use the indexOf() Function to Find Substring in Scala
  3. Use the matches() Function to Find Substring in Scala
  4. Use the findAllIn() Function to Find Substring in Scala
How to Find a Substring of String in Scala

This tutorial will discuss the procedure to find a substring in a Scala string.

A string is a sequence of characters and an array of chars. Scala provides a class, i.e., String, to handle the chars and their operations such as creating, finding, removing, etc.

This article will find the substring in a string by using some built-in functions and running examples.

Use the contains() Function to Find Substring in Scala

Here, we used the contains() function to find the substring in a string. This function returns a Boolean value, either true or false. See the example below.

object MyClass {
    def main(args: Array[String]) {
        val  str =  "This basket contains an apple"
        println(str)
        val isPresent = str.contains("apple")
        if(isPresent){
            println("Substring found")
        }else println("Substring not fount")
    }
}

Output:

This basket contains an apple
Substring found

Use the indexOf() Function to Find Substring in Scala

Here, we used the indexOf() function to get the index of the available substring. This function returns an integer value greater than 0 if a substring is present.

See the example below.

object MyClass {
    def main(args: Array[String]) {
        val  str =  "This basket contains an apple"
        println(str)
        val isPresent = str.indexOf("apple")
        if(isPresent>0){
            println("Substring found")
        }else println("Substring not fount")
    }
}

Output:

This basket contains an apple
Substring found

Use the matches() Function to Find Substring in Scala

We used the matches() function to search for an inaccurate substring. Sometimes we want to search some string but don’t know the full string and only know some part of it; then, we can use this function to match that part in the whole string.

This function returns a Boolean value, either true or false. See the example below.

object MyClass {
    def main(args: Array[String]) {
        val  str =  "This basket contains an apple"
        println(str)
        val isPresent = str.matches(".*apple*")
        if(isPresent){
            println("Substring found")
        }else println("Substring not fount")
    }
}

Output:

This basket contains an apple
Substring found

Use the findAllIn() Function to Find Substring in Scala

We used the findAllIn() function that takes the argument as a string. We used this function with the length property to get the length of found string.

It returns more than zero if the string is present. See the example below.

object MyClass {
    def main(args: Array[String]) {
        val  str =  "This basket contains an apple"
        println(str)
        val isPresent = "apple".r.findAllIn(str).length
        if(isPresent>0){
            println("Substring found")
        }else println("Substring not fount")
    }
}

Output:

This basket contains an apple
Substring found

Related Article - Scala String