×

Scala Tutorial

Scala Basics

Scala Control statements

Scala Functions

Scala Arrays

Scala Lists

Scala Strings

Scala Classes & Objects

Scala Collections

Scala String startsWith() Method with Example

By IncludeHelp Last updated : October 21, 2024

Description and Usage

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!

Load comments ↻





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