Methods for Comparing Strings
In the following lesson, you will be introduced to the different methods provided by Scala for comparing strings.
In a previous lesson, we touched upon String
comparison by discussing how to test the equality of strings using the equality operator ==
. In this lesson, we will look at a couple of built-in Scala methods which are specifically used for String
comparison. Let’s get right into the first method.
matches
By now, we’ve had quite a bit of practice with regular expressions. matches
is a method which compares a string with a regular expression; it matches a regular expression pattern with an entire string.
matches
is called on the string that you want to compare a regular expression with. It’s a boolean type and returns true
if the string matches and false
if it doesn’t. The syntax is as follows.
Let’s look at an example where we need to check if our string matches the regular expression ...