Scala String endsWith() Method with Example

Here, we will learn about the endsWith() method of String class in Scala. It is used to check for the presence of suffix substring in the string. We will see its working, syntax and example.
Submitted by Shivang Yadav, on February 04, 2021

String is an immutable collection that stores sequences of characters.

String endsWith() Method

The endsWith() method on strings is used to check whether the string ends with the given suffix substring passed as parameter to the method.

Syntax:

string_Name.endsWith(String subString)

Parameters: The method accepts a single parameter which the suffix substring to be checked in the string.

Return Value: It returns a Boolean value which is the result of the comparison operation.

Example 1:

object myObject {
    def main(args: Array[String]) {
        val myString = "Scala programming language"
        
        val isSuffixPresnt = myString.endsWith("language")
    
        if(isSuffixPresnt)
            println("The string ends with the given suffix!")
        else 
            println("The string does not end with the given suffix!")
    }
}

Output:

The string ends with the given suffix!

Example 2:

object myObject {
    def main(args: Array[String]) {
        val myString = "Scala programming language"
        
        val isSuffixPresnt = myString.endsWith("f")
    
        if(isSuffixPresnt)
            println("The strings ends with the given suffix!")
        else 
            println("The strings does not end with the given suffix!")
    }
}

Output:

The strings does not end with the given suffix!


Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.