Which of these methods is used to compare a specific region inside a string with another specific region in another string?

The Java Tutorials have been written for JDK 8. Examples and practices described in this page don't take advantage of improvements introduced in later releases and might use technology no longer available.
See Java Language Changes for a summary of updated language features in Java SE 9 and subsequent releases.
See JDK Release Notes for information about new features, enhancements, and removed or deprecated options for all JDK releases.

Show

The String class has a number of methods for comparing strings and portions of strings. The following table lists these methods.

Methods for Comparing Strings
Method Description
boolean endsWith(String suffix)
boolean startsWith(String prefix)
Returns true if this string ends with or begins with the substring specified as an argument to the method.
boolean startsWith(String prefix, int offset) Considers the string beginning at the index offset, and returns true if it begins with the substring specified as an argument.
int compareTo(String anotherString) Compares two strings lexicographically. Returns an integer indicating whether this string is greater than (result is > 0), equal to (result is = 0), or less than (result is < 0) the argument.
int compareToIgnoreCase(String str) Compares two strings lexicographically, ignoring differences in case. Returns an integer indicating whether this string is greater than (result is > 0), equal to (result is = 0), or less than (result is < 0) the argument.
boolean equals(Object anObject) Returns true if and only if the argument is a String object that represents the same sequence of characters as this object.
boolean equalsIgnoreCase(String anotherString) Returns true if and only if the argument is a String object that represents the same sequence of characters as this object, ignoring differences in case.
boolean regionMatches(int toffset, String other, int ooffset, int len) Tests whether the specified region of this string matches the specified region of the String argument.

Region is of length len and begins at the index toffset for this string and ooffset for the other string.

boolean regionMatches(boolean ignoreCase, int toffset, String other, int ooffset, int len) Tests whether the specified region of this string matches the specified region of the String argument.

Region is of length len and begins at the index toffset for this string and ooffset for the other string.

The boolean argument indicates whether case should be ignored; if true, case is ignored when comparing characters.

boolean matches(String regex) Tests whether this string matches the specified regular expression. Regular expressions are discussed in the lesson titled "Regular Expressions."

The following program, RegionMatchesDemo, uses the regionMatches method to search for a string within another string:

public class RegionMatchesDemo { public static void main(String[] args) { String searchMe = "Green Eggs and Ham"; String findMe = "Eggs"; int searchMeLength = searchMe.length(); int findMeLength = findMe.length(); boolean foundIt = false; for (int i = 0; i <= (searchMeLength - findMeLength); i++) { if (searchMe.regionMatches(i, findMe, 0, findMeLength)) { foundIt = true; System.out.println(searchMe.substring(i, i + findMeLength)); break; } } if (!foundIt) System.out.println("No match found."); } }

The output from this program is Eggs.

The program steps through the string referred to by searchMe one character at a time. For each character, the program calls the regionMatches method to determine whether the substring beginning with the current character matches the string the program is looking for.


This method has two variants which can be used to test if two string regions are equal.

Syntax

Here is the syntax of this method −

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

Parameters

Here is the detail of parameters −

  • toffset − the starting offset of the subregion in this string.

  • other − the string argument.

  • ooffset − the starting offset of the subregion in the string argument.

  • len − the number of characters to compare.

Return Value

  • It returns true if the specified subregion of this string matches the specified subregion of the string argument; false otherwise. Whether the matching is exact or case insensitive depends on the ignoreCase argument.

Example

import java.io.*; public class Test { public static void main(String args[]) { String Str1 = new String("Welcome to Tutorialspoint.com"); String Str2 = new String("Tutorials"); String Str3 = new String("TUTORIALS"); System.out.print("Return Value :" ); System.out.println(Str1.regionMatches(11, Str2, 0, 9)); System.out.print("Return Value :" ); System.out.println(Str1.regionMatches(11, Str3, 0, 9)); } }

This will produce the following result −

Output

Return Value :true Return Value :false

java_strings.htm

Which of these methods is used to compare a specific region inside a string with another specific region in another string?
Which of these methods is used to compare a specific region inside a string with another specific region in another string?
DownloadApp

Home » JAVA Programming » Strings » Question

  1. Which of these methods is used to compare a specific region inside a string with another specific region in another string?

    1. regionMatches()
    2. regionMatch()
    3. match()
    4. RegionMatches()
    5. None of these

regionMatches()

Which of these methods is used to compare a specific region inside a string with another specific region in another string?

Improve Article

Save Article

Like Article

The regionMatches() method of the String class has two variants that can be used to test if two string regions are matching or equal. There are two variants of this method, i.e., one is case sensitive test method, and the other ignores the case-sensitive method.

Syntax:

1. Case sensitive test method:

public boolean regionMatches(int toffset, String other, int offset, int len)

2. It has the option to consider or ignore the case method:

public boolean regionMatches(boolean ignoreCase, int toffset, String other, int offset, int len)

Parameters:

  • ignoreCase: if true, ignore the case when comparing characters.
  • toffset: the starting offset of the subregion in this string.
  • other: the string argument being compared.
  • offset: the starting offset of the subregion in the string argument.
  • len: the number of characters to compare.

Return Value:

A substring of the String object is compared to a substring of the argument other. The result is true if these substrings represent character sequences that are the same, ignoring case if and only if ignoreCase is true. The substring of this String object to be compared begins at index toffset and has length len. The substring of other to be compared begins at index offset and has length len. The result is false if and only if at least one of the following is true

Example 1:

import java.io.*;

class CheckIfRegionsEqual {

    public static void main(String args[])

    {

        String str1

            = new String("Welcome to Geeksforgeeks.com");

        String str2 = new String("Geeksforgeeks");

        String str3 = new String("GEEKSFORGEEKS");

        System.out.print(

            "Result of Comparing of String 1 and String 2: ");

        System.out.println(

            str1.regionMatches(11, str2, 0, 13));

        System.out.print(

            "Result of Comparing of String 1 and String 3: ");

        System.out.println(

            str1.regionMatches(11, str3, 0, 13));

        System.out.print(

            "Result of Comparing of String 2 and String 3: ");

        System.out.println(

            str2.regionMatches(0, str3, 0, 13));

    }

}

OutputResult of Comparing of String 1 and String 2: true Result of Comparing of String 1 and String 3: false Result of Comparing of String 2 and String 3: false

Example 2:

import java.io.*;

class CheckIfRegionsEqual {

    public static void main(String args[])

    {

        String str1 = new String("Abhishek Rout");

        String str2 = new String("abhishek");

        String str3 = new String("ABHISHEK");

        System.out.print(

            "Result of comparing String 1 and String 2 : ");

        System.out.println(

            str1.regionMatches(true, 0, str2, 0, 8));

        System.out.print(

            "Result of comparing String 1 and String 3 : ");

        System.out.println(

            str1.regionMatches(false, 0, str3, 0, 8));

        System.out.print(

            "Result of comparing String 2 and String 3 : ");

        System.out.println(

            str2.regionMatches(true, 0, str3, 0, 8));

    }

}

OutputResult of comparing String 1 and String 2 : true Result of comparing String 1 and String 3 : false Result of comparing String 2 and String 3 : true

Note: The method returns false if at least one of these is true,

  • toffset is negative.
  • offset is negative.
  • toffset+len is greater than the length of this String object.
  • ooffset+len is greater than the length of the other argument.
  • ignoreCase is false, and there is some nonnegative integer k less than len such that:
this.charAt(toffset+k) != other.charAt(ooffset+k)
  • ignoreCase is true, and there is some nonnegative integer k less than len such that:
Character.toLowerCase(Character.toUpperCase(this.charAt(toffset+k))) != Character.toLowerCase(Character.toUpperCase(other.charAt(ooffset+k)))