Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

scala

What is the regionMatches method in Scala?

Sheza Munir

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

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:

  1. ignoreCase: Boolean value used to change the case sensitivity options of the function.
  2. toOffSet: This is an integer for offset.
  3. string2: This is the string to be compared.
  4. index: The starting index of the region in the first string to check for matching.
  5. 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 method
val 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 method
val 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.

RELATED TAGS

scala

CONTRIBUTOR

Sheza Munir
Copyright ©2022 Educative, Inc. All rights reserved

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring