Home »
Scala
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'