Home »
Scala
Scala String matches() Method
By IncludeHelp Last updated : October 21, 2024
Description and Usage
The matches() method on strings is used to check whether the regular expression passed as parameter is present in the string or not.
Syntax
string_Name.matches(String regex)
Parameters
The method accepts a single parameter which is a regular expression to be check for in the string.
Return Value
It returns a Boolean value based on the presence of the regular expression in string.
Example 1
object myObject {
def main(args: Array[String]) {
val string1 = "includehelp"
val ismatched = string1.matches(".*help")
if(ismatched){
println("The string contains the regular expression...")
}
else{
println("The string does not contains the regular expression...")
}
}
}
Output
The string contains the regular expression...
Example 2
object myObject {
def main(args: Array[String]) {
val string1 = "includehelp"
val ismatched = string1.matches(".*f")
if(ismatched){
println("The string contains the regular expression...")
}
else{
println("The string does not contain the regular expression...")
}
}
}
Output
The string does not contain the regular expression...