×

Java String Matches vs Contains

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

Related Topics

How to run Java Program in Eclipse

How to run Java Program in Eclipse In this section, we will learn how to write, save, compile, and execute or run a Java program in Eclipse. Eclipse is one of...

2 minutes read.

Java Database Connectivity with Oracle

JDBC: A Programmer can develop a complete application using the Java built-in API’s. So, for storing the data required for solving a real-world problem is stored into a database. To connect...

5 minutes read.

Java Clone Array

We frequently need to copy an array to back up its original components. We have a few unique numbers and strings, including Armstrong numbers, palindrome numbers, and palindrome strings. To...

4 minutes read.

Difference between next() and nextline() in Java

One of the simplest methods for receiving input of the basic data types, also including int, double, and strings, in Java, is to use the Scanner class, which is part...

3 minutes read.

Java Lock

A lock is indeed a threaded synchronization technique similar to Java's synchronized blocks, however, locking can be more complex. It's not like we can completely get rid of the synchronized...

5 minutes read.

Addition Program in Java

Addition Program in Java We can perform the addition (sum) of two or more numbers by using the arithmetic operator (+). To write an addition program in Java, one must understand...

3 minutes read.

Java IO file not found exception

One of the exception classes offered by the java.io package is FileNotFoundException. An exception is raised when we attempt to access a file that isn't present in the system. It...

4 minutes read.

Java String indexOf() method

Java String indexOf() method returns index of a given character or substring present in a String. Methods Description int indexOf(int ch)     It returns index position for the given char value.int indexOf(int ch,...

2 minutes read.

How to Convert String to Date in Java

How to Convert String to Date in java You can convert String to Date in Java by using the parse() method. There are two classes which have parse() method namely, DateFormat and SimpleDateFormat classes....

2 minutes read.

Java Map Generic

Java arrays maintain an ordered collection of things, and the index can be used to access the data (an integer). Unlike HashMap, which stores data as a Key/Value pair. We...

3 minutes read.

Java Instanceof Keyword

To determine whether an object is an instance of the supplied type in Java, use the instanceof operator (class or subclass or interface). The type of comparison in java differentiates the...

4 minutes read.

How to run java program in ubuntu

To run the java programming in ubuntu, we need to follow several steps: Let's see the process. Step1: Install the Java compiler or JDK to the system. To run the java program, we...

3 minutes read.

What is interpreter in Java?

The programming language Java is platform-neutral. Therefore, can use Java on any platform that supports the Java processor. The Piece of software transforms the Java bytecode contained in the class...

5 minutes read.

How to Convert String to Integer in Java

How to convert String to int in Java You need to convert String into int if you want to perform a mathematical operation on string which contains digits. To do so,...

3 minutes read.

Application of Array in Java

In this article we are going to acknowledge about what the array is, types of arrays and their applications. What is an array? An array is often a set of interrelated elements...

4 minutes read.

How to Reverse a String in Java

How to Reverse a String in Java There are a lot of ways to reverse a string in Java. One can use iteration, StringBuilder, StringBuffer to do the reverse of a...

6 minutes read.

Console Errors in Java

An unlawful motion taken through the person that reasons this system to act abnormally is amistake until this system is compiled or run; maximum programming mistakes pass unnoticed.The software is...

3 minutes read.

Java vs Node.js

Java: Java is an object oriented programming language. It is also known as multi threaded language. It was designed by James gosling in the year 1995. We can also say that...

4 minutes read.

Dutch National Flag Problem in Java

Dutch National Flag (DNF) is a programming issue that Edsger Dijkstra put up. The white, red, and blue hues make up the Dutch flag. The goal is to haphazardly set...

6 minutes read.

Coin change problem in dynamic programming

In this tutorial, we will understand a popular problem called the coin changeproblem through dynamic programming. This problem checks the logical and critical thinking ability of the person. Dynamic Programming...

5 minutes read.