×

String Matches in Java

Firstly we have to know something about String?

Strings are a bundle of different characters that are normally used in Java programming language. Strings are regarded as objects in the Java programming language.

“String” is a Java platform class that allows you to construct and handle strings.

Now take a look that how we Creating Strings

The normal way to create a string in Java is to simply write:-

String s = “Hello World”;

The compiler constructs a String object with the value "Hello world!" whenever it detects a string phrase in your code.

String objects cannot be made by using the new keyword and a function Object, just like any other object. The String class provides 11 constructors that allow you to set the string's initial value from a variety of sources, including an array of characters.

Another way to create a string is:-

String s = new String (“Hello World”);

Now let’s come to the main topic.

String matches :

Variants of the matches() method are being used to check whether a given text matches a regular expression or not. Regardless of whether this method is called matches() or matches(), where we pass two arguments, our string and our regular expression, the working and output stay the same.

There are three variations of the matches() methods, which are mentioned and explained below:

String matches()

This method determines whether the provided string satisfies the regular expression. The expression Pattern.matches gets the very same result as invoking this function with the form str.matches(regex).

Syntax: 

public boolean matches(String regex) 

Return Type:

Boolean Value, If and only if strings satisfy the specified regular expression, this value is true; otherwise, it is false.

Parameter;

String is to be matched in a regular expression.

Take a look on an Example:

// Program to Illustrate the operation of Matches() Method

// Main class

public class GFG {
public static void main(String args[]){
// Declaring and initializing a string
String Str = new String("Welcome to Java");
System.out.print(
"Does String contains regex (.*)java(.*) ? : ");
// Analising if regex is present or not
System.out.println(Str.matches("(.*)java(.*)"));
System.out.print(
"Does String contains regex java ? : ");
// Analising if regex is present or not
System.out.println(Str.matches("java"));
}
}

OUTPUT:

Does String contains regex (.*)java(.*) ? : true
Does String contains regex java ? : false

String regionMatches()

Variants of regionMatches() method


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

Parameters:

regionMatches

  • other:  the argument of string.
  • Int offset: the starting offset of the subregion in the string argument.
  • int toffset:  the starting offset of the subregion in this string.
  • Int len: different types of characters for comparison.

Return Value:

This function returns true if the given subregion of this string satisfies the specified subregion of the string argument; else, it returns false. The ignoreCase option determines whether the matching is accurate or case insensitive.

Take a look on an Example:

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

OUTPUT:

Return Value : true
Return Value : False

Syntax:

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

 String regionMatches() With ignoreCase

This approach offers two variations for determining whether two string segments are equal.

Parameters:

  • Int toffset:  the starting offset of the subregion in this string.
  • other:  the argument of string.
  • Int offset: the starting offset of the subregion in the string argument.
  • Int len: different types of characters for comparison.
  • ignoreCase – if it is true, ignore the case when comparing with characters.

Return Value:

If this string's stated subregion satisfies the string argument's specified subregion, it gives true; otherwise, it gives false. The ignoreCase argument determines whether the matching is exact or case-insensitive.

Take a look at an example:

import java.io.*;
public class Test {


   public static void main(String args[]) {
      String Str1 = new String("Welcome to my world");
      String Str2 = new String("HELLOWORLD");


      System.out.print("Return Value :" );
      System.out.println(Str1.regionMatches(true, 11, Str2, 0, 10));
   }
}

Output:

Return value : True

Syntax:

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

Related Topics

Java Integer parseUnsignedInt() method

The parseUnsignedInt() method of Java Integer class parses the string argument as an unsigned decimal integer. The second parameter parses the string argument as an unsigned integer in the radix specified...

2 minutes read.

Bellman Ford Algorithm in Java

Numerous algorithms have been used in dynamic programming to determine the shortest path inside a graph. Among them are Floyd, all-pair shortest path problem, Breadth First Search, Depth First Search,...

6 minutes read.

Applet Life Cycle in Java

In this article, we are going to acknowledge you about what a applet is, what is its life cycle and stages in life cycle, along with the syntax and example...

4 minutes read.

Jdoodle Java

Everything is instantaneous and fast-paced in the modern world. Online compilers available via the internet are immensely helpful for programmers seeking to learn a new programming language but lacking the...

4 minutes read.

Brilliant Number in Java

It is a number N that is made up of two prime numbers that have the same number of digits and is called a brilliant number. Several/Some of the brilliant Numbers...

3 minutes read.

How to make Java Projects

Ant and Maven are both offered by NetBeans for the development of Java applications. When using Ant, the IDE creates an Ant build script depending on the settings you select...

6 minutes read.

Enterprise Java Beans

One of the many Java APIs for the common development of corporate software is Enterprise Java Beans (EJB). An EJB, a server-side software component, contains the business logic of an...

4 minutes read.

How to Create Singleton Class in Java

In this tutorial, we will discuss the singleton class and how to create it. Introduction Java is a purely object-oriented programming language. It consists of classes and objects. But Singleton is one...

4 minutes read.

Find next greater number with same set of digits in Java

It contains a number (num). Finding the smallest number that has the same number of elements as num that is also larger than num is the task at hand. If...

8 minutes read.

Can we override Private Method in Java

In this article we will learn the concept of override, private method in java and we will now whether we can override private method in java or not. Override in Java Any...

3 minutes read.

Advanced Java skills

These were some of the contemporary talents that a Java developer should master in efforts to progress in the era of science in 2022. A database such as MySQL, a...

4 minutes read.

Menu Driven Program in Java

Menu Driven Program in Java The menu-driven program in Java is a program that displays a menu and then takes input from the user to choose an option from the displayed...

3 minutes read.

Java String format() method

format() method returns a formatted String based on the given locale,specified format and arguments. Syntax: public static String format(String format , Object… args) Parameter: locale : It specifies locale value to be applied on...

2 minutes read.

Types of JDBC Drivers

JDBC Drivers: A piece of software known as JDBC Driver permits database communication between Java applications and the server. In order to communicate with our database server, JDBC drivers put into practice...

4 minutes read.

Difference Between Access Specifiers and Modifiers in Java

Java employs access modifiers to restrict a class's data members, member functions, and constructor. Access modifiers are essential when creating Java program and applications. Access modifiers in Java include: defaultpublicprotectedprivate Default Access Modifiers Without...

4 minutes read.

Thread Safety and How to Achieve it in Java

Before diving into the topic, let’s just recap the concept of Multithreading provided by Java where we can create and execute multiple threads of the same object. When these multiple...

5 minutes read.

Fibonacci Series Program in Java

Fibonacci Series Program in Java using Recursion Fibonacci series is a series whose every term is comprised of adding its previous two terms, barring the first two terms 0 and 1....

3 minutes read.

How to Run Applet Program in Java

An applet is a new type of program which is put in a webpage. By inserting applet into a webpage dynamic content can be created. Characteristics of an Applet The applet is...

11 minutes read.

Vectors in Java

Vector Class We may make resizable arrays comparable to the ArrayList class using the Vector class, which implements the List interface. A vector is similar to a dynamic collection that can...

4 minutes read.

Exception Handling in Java

Before looking at how exceptions are handled in java, it is necessary to see what an exception is. What is Exception? Whenever a program is written, errors are encountered. Some of these...

6 minutes read.