×

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

Sorting a String in Java

Sorting a String in Java Sorting is a process of arranging available data objects in a meaningful format that is ascending or descending order. Sorting a string or array of String...

4 minutes read.

Java Viva Questions and Answers

Question: Explain Java programming language. Answer: It is a high-level programming language. This programming language is based on the principles of object-oriented programming. Large-scale applications can be developed by using this...

16 minutes read.

Generic Linked List in Java

A linear data structure known as a Linked List stores values in nodes. As we already know, each node has two properties: its value and a link to the node...

6 minutes read.

Internal Working of ArrayList in Java

Java's version of a resizable array is called ArrayList. The dynamic growth of an array list ensures that there is always room for new elements. ArrayList's backing data structure is...

8 minutes read.

Difference between String, StringBuffer and StringBuilder in java

What is a string in Java? Strings are a collection of characters that are commonly used in Java programming. Strings are regarded as objects in the Java programming language. “String” is a...

4 minutes read.

Java InputStreamReader

What is InputStreamReader?An InputStreamReader is a converter between byte and character streams: It reads bytes and converts them to characters with the help of a charset. The charset it uses can...

4 minutes read.

Centered Square Numbers in Java

In this tutorial, we will understand how to check the number is a centered square number. It is one of the prevalent interview questions of IT companies. Firstly, we will...

3 minutes read.

Java 13 New Features

The Java SE Platform's JDK 13 version is an open-source reference implementation defined by the Java Community's JSR 388 Process. The necessary modifications made in Java 13 are listed below....

6 minutes read.

How to Convert Binary to Decimal in Java

How to Convert Binary to Decimal in Java There are two methods to convert binary to decimal. Using Integer.parseInt() method Using user-defined logic Using Integer.parseInt() method The parseInt() is a static method of...

2 minutes read.

Class definition in Java

The class definition in Java Java is an object-oriented programming language. We essentially know that programming languages based on object-oriented paradigms have classes and objects in their concepts as main, which...

6 minutes read.

Access Modifier in Java

The access modifiers in java are used to change the accessibility and scope of a method, constructor, class, and fields. If you are aware of C++ language, when we declare any member...

2 minutes read.

Java Developer

Who is a Java Developer? A Java developer is a skilled programmer who works on commercial applications, software, and webpages.  Java developers can work in two different areas: Operating system development:...

3 minutes read.

How to Convert String to char in Java

How to Convert String to char in Java There are two methods to convert String to char are: Using charAt() method Using tocharArray() method Using charAt() method This is the method of String class that...

3 minutes read.

Java Location

PATH is an environmental variable that the system software now uses to find executable files (.exe) or Java binaries ( java or javac command). Once chosen, a route cannot be...

3 minutes read.

Difference between Static and Instance Methods in Java

Static Method in Java The static technique has a place with the class as opposed to the object of the course. These are intended to be divided between every one of...

10 minutes read.

Parallel Arrays Sort in Java

Sorting is a technique of arranging a sequence of numbers in ascending order, that is, the first number being lowest and gradually incrementing the numbers such that the last number...

7 minutes read.

Java Session

Session indicates interval of time. A session is a simple  time interval in which servers and client interacts. To maintain the state of the client or user we use technologies...

5 minutes read.

Java Math hypot() Method

The hypot() method of Math class returns the square root for the expression x2 + y2 without the intermediate underflow or overflow . Syntax: public static double hypot(double x, double y) Parameters: The parameters...

2 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.

Java Program to print even and odd numbers using 2 threads

Using two threads in a single thread is even odd printing in Java programming with multiple threads. To create code that prints even and odd using two threads, we must...

2 minutes read.