What is the regionMatches method in Scala?
Scala offers many functions for string manipulation. The regionMatches function is one of the string comparison methods.
We can use the regionMatches function to compare two strings and determine if a specified portion is identical, or if there are any differences.
The function is case sensitive, but that can be toggled by setting the ignoreCase argument to false.
Syntax
Boolean regionMatches(boolean ignoreCase, int toOffset, String string2, int index, int len)
Parameters
The regionMatches function takes multiple arguments, outlined below:
ignoreCase: Boolean value used to change the case sensitivity options of the function.toOffSet: This is an integer for offset.string2: This is the string to be compared.index: The starting index of the region in the first string to check for matching.len: This is the length of the matching region.
Return value
The regionMatches function returns a Boolean value, whether the string matches (returns true) or it does not (returns false).
Code
The following code snippet shows the implementation of the regionMatches function.
object Main extends App{// Applying regionMatches methodval check = "This is an Edpresso shot.".regionMatches(true, 0, "Edpresso shot", 11, 8)if (check){println("Region matches.")} else{println("Region does not match.")}// Applying regionMatches methodval check_2 = "This is an Edpresso shot.".regionMatches(true, 0, "this", 0, 4)if (check_2){println("Region matches.")} else{println("Region does not match.")}}
In the above code, the first example evaluates to False. The string to be matched, "Edpresso shot", is not the same as "Edpresso", which begins at index 11 and has a length of 8.
In the second example, "this" matches with "This" because the boolean flag is set to True, and case sensitivity does not matter.
Free Resources