Java String Matches vs Contains – TutorialAndExample

String Matches in Java

The matches() function and its variations are used to determine whether or not a provided text matches a regular expression. The functioning as well as output of an expression remains the same where we supply two arguments: string and routine expression.

The matches() function has three versions, which are listed and described below:

String Matches()

By using this function, you may find out if the supplied string matches the regular expression. The outcome of using this function with the formed str.matches is exactly the same as that of the expression Pattern.matches (regex).

Syntax: 

public boolean matches(String regex) 

Return Type: Value Boolean, this value is true if and only if the given regular expression is satisfied by the strings; elsewhere, it is false.

Parameter: Use a regular expression to match a string.

Example:

public class TAE {
  // Main driver method
  public static void main(String args[])
  {
    // Declaration and initialization of a string
    String Str = new String("Welcome to tutorialandexample");
    // Shows a comment for better readability
    System.out.print(
      "Does String contains regex (.*)tutor(.*) ? : ");
    // Checking if regex is present or not
    System.out.println(Str.matches("(.*)tutor(.*)"));
    // Shows a comment for better readability
    System.out.print(
      "Does String contains regex tutor ? : ");
    // Checking if regex is present or not
    System.out.println(Str.matches("tutor"));
  }
}

Output:

Java String Matches vs Contains

String regionMatches()

Variants of regionMatches()method

RegionMatches(int toffset, int len, other, int offset)

RegionMatches(int toffset, other, ignore case, int offset, int len)

Parameters

regionMatches

Other:  The argument or comment of string is called other.

Int offset: The string parameter’s string argument is the subregion’s beginning offset.

int toffset:  The subregion’s initial offset is included in this string.

Int len: Different type of characters for comparision.

Return Value

If the provided subregion of the string argument is satisfied by the given subregion of this string, this method returns true; otherwise, it returns false. The ignoreCase option determines that the matching is accurate or not.

Example:

public class Test {
   public static void main(String args[]) {
      String Str1 = new String("Welcome to tutorialandexample");
      String Str2 = new String("tutorialandexample");
      String Str3 = new String("TUTORIALANDEXAMPLE");
      System.out.print("Return Value :" );
      System.out.println(Str1.regionMatches(11, Str2, 0, 18));
      System.out.print("Return Value :" );
      System.out.println(Str1.regionMatches(11, Str3, 0, 18));
   }
}

OUTPUT

Java String Matches vs Contains

Syntax:

public boolean regionMatches(int str_strt, String other, int other_strt,int len)

String regionMatches() With ignoreCase

There are two ways to test whether 2 string parts are equal using this method.

Parameters

  • Int toffset: The subregion’s initial offset is included in this string.
  • other:  the normal argument of string.
  • Int offset: The string parameter’s string argument is the subregion’s beginning offset.
  • Int len: Different type of characters for comparison.
  • ignoreCase: In that situation, while comparing with characters, disregard the case.

Return Value

It returns true if the string argument’s stated subregion is satisfied by the subregion mentioned in this string; otherwise, it returns false. Whether or whether the matching is exact depends on the ignoreCase argument.

Example

public class Test {
   public static void main(String args[]) {
      String Str1 = new String("Welcome to Tutorialsandexample.com");//String first
      String Str2 = new String("TUTORIALS");//String Second
      System.out.print("Return Value :" );
      System.out.println(Str1.regionMatches(true, 11, Str2, 0, 9));
   }
}

Output

Java String Matches vs Contains

 

Syntax:

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

Java String contains()

This string’s character order is checked using the contains() function of the Java String class. If the string’s series of char values can be discovered, it returns true; otherwise, it returns false.

Syntax

public boolean contains(CharSequence sequence)
Implementation:
public boolean contains(CharSequence sequence)
{
   return indexOf(sequence.toString()) > -1;
}

Here, the CharSequence is converted to a String before the indexOf function is used. If the String is found, the method indexOf returns zero or a larger number; otherwise, -1 is returned. The contains() function then returns true if a series of char values exists, else it returns false.

Example:

class dr{
	public static void main(String args[])
	{
		String s1 = "My father is a DR.”
		// True
		System.out.println(s1.contains("DR."));


		// False
		System.out.println(s1.contains("er"));
	}
}

Output:

Java String Matches vs Contains