Does String have a include method

for instance:

scala> "hello world".include("^hello")

It accepts a regex and returns true for this case.
I know there is matches but it must accept the full matching regex.


Using the methods of the String class that accept a pattern as a String is not really efficient, since every time you call that method, it has to parse and compile the regex.

There is a convenience class in Scala which you should use:

import scala.util.matching._

val r1 = "hello".r // this regex is automatically anchored (enclosed in ^ and $) !
val r2 = "^hello".r.unanchored // this is not

if (r2.matches("hello, world")) {
1 Like

hello, scala 2.12.15 doesn’t work with your code.
do you mean scala 2.13 version?

They added matches at some point. You could always just pattern match, as shown in the scaladoc.

1 Like

It is possible to click for “other versions” on the main page and find

They don’t change API during point releases.