Java regionMatches Method

The Java regionMatches method is a Java String Method, which 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. The syntax of the String.regionMatches in Java Programming language is

Java regionMatches Method syntax

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

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, Java = java = JAVA = JaVa.

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, Java regionMatches 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 Case-insensitive match. Otherwise, it will perform 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 Java string.regionMatches method to check whether two string regions are equal or not.

// Java String.regionmatches example
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 Java = Java, which is true.

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

Next, we checked for non-equivalent values using this Java regionMatches method. Here “Learn” is not at all matches 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 is returning True because this method is performing 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 Java program, We are going to ask the user to enter any word. Based on the user entered string value, the Java 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 Java Programming Language");
		}
		else {
			System.out.println("Goodbye to Java Tutorials");
		}
	}
}
Please Enter any word: 
JAVA
Welcome to Java Programming Language

Let us enter different word

Please Enter any word: 
Hello
Goodbye to Java Tutorials

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

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

Next, we used the Java 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 Java Programming Language”); statement printed.
  • Else System.out.println(“Goodbye to Java Tutorials”); statement printed.