The method regionMatches() checks if two String regions are equal.

Here are a few important points:

  • ignoreCase specifies whether we should ignore the case of both Strings
  • toffset determines the starting index of the first String
  • other specifies the second String.
  • ooffset specifies the starting index of the second String
  • len specifies the number of characters to compare

Available Signatures

boolean regionMatches(int toffset, String other, int ooffset, int len)
boolean regionMatches(boolean ignoreCase, int toffset, String other, int ooffset, int len)

Example

@Test
public void whenCallRegionMatches_thenCorrect() {
    assertTrue("welcome to baeldung".regionMatches(false, 11, "baeldung", 0, 8));
}
Next »
Java String.replace()
« Previous
Java String.lastIndexOf()
Course – LS – All

Get started with Spring and Spring Boot, through the Learn Spring course:

>> CHECK OUT THE COURSE
res – REST with Spring (eBook) (everywhere)
Comments are closed on this article!