Java regionMatches Method

The Java regionMatches method is a String Method that tells whether this string region matches the user-specified string region or not. Based on the result, the Java regionMatches function will return Boolean True or False.

Let us write a string regionMatches in Java Programming language example and the syntax of the method is

Java regionMatches Method syntax

The Java Programming language provides two different regionMatches methods to check region equality. The following regionMatches method will accept four arguments to check whether the two string regions are equal or not. It will perform a case-sensitive match means, Name != name.

public boolean regionMatches(int Starting_Index, String other, int offset, int len); // It will return boolean True or False 

//In order to use in program
String_Object.regionMatches(int Starting_Index, String other, int offset, int len)

The following Java regionMatches method will accept five arguments to check whether the two string regions are equal or not. It will perform case-insensitive match means, Name = name = NAME = NaMe.

public boolean regionMatches(boolean ignorecase, int toffset, String other, int offset, int len); // It will return boolean True or False 

//In order to use in program
String_Object.regionMatches(boolean ignorecase, int toffset, String other, int offset, int len)
  • String_Object: A valid string object that you want to use in the region match.
  • Starting_Index: Please specify the starting index position of the String_Object. If you specify this value as zero, then the regionMatches method will match from index position 0. Otherwise, it will start matching from the character at the specified index position.
  • Other: Please specify another string object that you want to use in the region match.
  • Offset: Please specify the starting index position of the Other. If you specify this value as zero, then the regionMatches String function will match from index position 0. Otherwise, the regionMatches function will start matching from the character at the specified index position.
  • Len: Please specify the length of a substring from the Other.
  • IgnoreCase: If you specify TRUE, Java regionMatches method will perform a Case-insensitive match. Otherwise, it will perform a Case-Sensitive match.

For example, if we specify the statement as: “Tutorial gateway”.regionMatches(9, “gateway”, 0, 7) will return True. Here String_Object = “Tutorial gateway”, Starting_Index = 9, Other = “gateway”, Offset = 0 and length = 7

Java regionMatches Example

In this example, we use the string.regionMatches method to check whether two string regions are equal or not.

package StringFunctions;

public class RegionMatchesMethods {
	public static void main(String[] args) {
		String str1 = "Java Programming";
		String str2 = "Java Programming language";
		String str3 = new String("Java");
		String str4 = new String("Learn Java");
		String str5 = new String("Tutorial Gateway");
		String str6 = new String("GATEWAY");
		
		boolean a = str1.regionMatches(0, str3, 0, 4);
		boolean b = str1.regionMatches(0, str4, 6, 4);
		boolean c = str1.regionMatches(5, str2, 5, 11);
		
		boolean d = str3.regionMatches(0, str4, 0, 5);
		boolean e = str5.regionMatches(9, str6, 0, 7);
		boolean f = str5.regionMatches(true, 9, str6, 0, 7);
		
		System.out.println("Does the String str1 matches to str3? = " + a);
		System.out.println("Does the String str1 matches to str4? = " + b);
		System.out.println("Does the String str1 matches to str2? = " + c);
		System.out.println("Does the String str3 matches to str4? = " + d);
		System.out.println("Does the String str5 matches to str6? = " + e);
		System.out.println("Does the String str5 matches to str6? = " + f);
	}
}
Java regionMatches Method 1

Within this Java regionMatches example, the following statement will call the public boolean regionMatches(int Starting_Index, String other, int offset, int len) method to compare the string str1 with str3.

From the above screenshot, you can observe that it is returning True. Here String_Object = “Java Programming”, Starting_Index = 0, Other = “Java”, Offset = 0 and length = 4. It means both are true.

boolean a = str1.regionMatches(0, str3, 0, 4);

Next, we checked for non-equivalent values using this regionMatches method. Here “Learn” is not at all match with “Java”

boolean d = str3.regionMatches(0, str4, 0, 5);

The following Java regionMatches statement will compare the region of string str5 with str26. Although the strings str5 and str6 are equal, from the above screenshot, you can observe that it is returning False. It is because both str5 and str6 differ in Case.

boolean e = str5.regionMatches(9, str6, 0, 7);

The following statement will call the public boolean regionMatches(boolean ignoreCase, int Starting_Index, String other, int offset, int len) method to compare the string str5 with str6. From the above screenshot, see that it returns True because this method performs a case-insensitive match.

boolean f = str5.regionMatches(true, 9, str6, 0, 7);

Lastly, we used the Java System.out.println statements will print the output.

Java regionMatches Example 2

In this program, We are going to ask the user to enter any word. Based on the user entered string value, the regionMatches function will display the message.

package StringFunctions;
import java.util.Scanner;

public class RegionMatchesMethods2 {
	private static Scanner sc;
	public static void main(String[] args) {
		sc = new Scanner(System.in);	
		String str = new String("Learn java program");
		System.out.println("Please Enter any word: ");
		String str1 = sc.nextLine();
		
		if (str.regionMatches(6, str1, 0, 4)|| str.regionMatches(true, 6, str1, 0, 4)) {
			System.out.println("Welcome to Programming Language");
		}
		else {
			System.out.println("Goodbye to Tutorials");
		}
	}
}
Please Enter any word: 
JAVA
Welcome to Programming Language

Let us enter a different word.

Please Enter any word: 
Hello
Goodbye to Tutorials

The following statement will ask the user to enter any word. Then we are going to assign the user entered value to the string variable str.

System.out.println("Please Enter any word: ");
String str1 = sc.nextLine();

Next, we used the If Else Statement to compare the user entered string region with the String Object “java programs” and check whether they are equal or not.

Next, we used both public boolean regionMatches(int Starting_Index, String other, int offset, int len) and public boolean regionMatches(boolean ignoreCase, int Starting_Index, String other, int offset, int len) to make sure that we are performing case-insensitive region match.

if (str.regionMatches(6, str1, 0, 4)|| str.regionMatches(true, 6, str1, 0, 4)) {
  • If the statement inside the If is True, System.out.println(“Welcome to Programming Language”); statement printed.
  • Else System.out.println(“Goodbye to Tutorials”); statement printed.