Scala String startsWith() Method with Example

Here, we will learn about the startsWith() method in Scala. The startsWith() method is used to check if the current string starts with the string in parameters. We will see syntax and working examples.
Submitted by Shivang Yadav, on January 27, 2021

String is an immutable collection that stores sequences of characters.

String startsWith() Method

The startsWith() method in Scala is used to check whether the calling string starts with the string inside the parameter.

Syntax:

string_name.startsWith(startString)

Parameters: The method accepts a single parameter which is a string to be checked for starting with.

Return Value: It returns a Boolean value based on whether the string starts with startString.

Example 1: Program to illustrate weather the given string is present in the calling staring

object MyClass {
    def main(args: Array[String]) {
        val myString = "ScalaProgrammingLanguage"
        println("The string is " + myString)
    
        val startsWith = myString.startsWith("Scala")
        
        if(startsWith)
            println("The string starts with 'Scala' ")
        else
            println("The string does not starts with 'Scala' ")
    }
}

Output:

The string is ScalaProgrammingLanguage
The string starts with 'Scala'

Example 2: Program to illustrate weather the given string is present in the calling staring

object MyClass {
    def main(args: Array[String]) {
        val myString = "ScalaProgrammingLanguage"
        println("The string is " + myString)
    
        val startsWith = myString.startsWith("program")
    
        if(startsWith)
            println("The string starts with 'program' ")
        else
            println("The string does not starts with 'program' ")
    }
}

Output:

The string is ScalaProgrammingLanguage
The string does not starts with 'program'




Comments and Discussions




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.