×

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

Block Swap Algorithm for array rotation in Java

An array and r, the rotation factor by which the array must be rotated, are both provided to us. We must then return the rotated array. A well-known and popular method is...

4 minutes read.

Scanner in Java

Static way of Programming: When a variable can’t change its value during run time is called a Static way of programming. In this programming a variable is directly assigned to a...

4 minutes read.

How to remove last character from String in Java

In java, there are predominantly three classes connected with the string. The classes are String, StringBuilder, and StringBuffer class that gives techniques connected with string control. Eliminating the first and...

5 minutes read.

Java String replaceAll() method

Java String replaceAll() method returns a String replacing all the sequence of characters matching regular expression i.e regex and replacement string. Syntax: public String replaceAll(String regex, String replacement) Parameters: regex : regular expression replacement :...

1 minute read.

AbstractSet Class in Java

The AbstractSet class is used for the implementation of the Abstract Collection class and interface. It is the part of Collection Frameworks. In the AbstractSet, the implementation is same as the...

3 minutes read.

Java Math ulp() Method

The ulp() method of Java Math class returns the size of an ulp of the argument. Syntax: public static double ulp(double x)public static float ulp(float x) Parameters: The parameter ‘x’ represents the floating-point number...

2 minutes read.

Contextual keywords in Java

Contextual keywords were earlier known as restricted identifiers and restricted keywords. Context keywords are chosen based on their expected placement in the syntactic grammar. These are the keywords in the code...

3 minutes read.

Check whether Java is installed or not

As we know that there are various operating systems, to check whether Java is installed or not in Windows and Mac we use the following ways.           Windows Operating System: There are several...

2 minutes read.

How to Convert boolean to String in Java

How to Convert boolean to String in Java There are two methods to convert boolean to String. Using valueOf(boolean) method Using toString(boolean) method For both the above methods, if the passes Boolean...

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

Facts about null in Java

Nearly all programming languages have a relationship with null. Hardly any programmers are unconcerned by null. The null has a java.lang.NullPointerException association in Java. Given that it is a class...

4 minutes read.

Minimum Number of Platforms Required for a Railway Station

The train station problem is one of the most significant problems typically posed in the programming round interview to gauge a candidate's aptitude for logic and problem-solving. Problem of Railway Station The...

6 minutes read.

Java Math min() Method

The min() method of Math class returns the smaller of two arguments. The arguments can be of double, float, int or long data type. Syntax: public static double min (double a, double...

2 minutes read.

Tribonacci series in Java

The Fibonacci series and the Tribonacci sequence are linked. The Fibonacci sequence, each element summates the three preceding terms, is expanded into the Tribonacci series in Java. Through specific examples,...

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

Java String trim() method

Java String trim() method returns String after eliminating leading and trailing spaces. Note:- Java String trim() method doesn't trim or omit middle spaces. Syntax: public String trim() Returns: It returns string with omitted leading and...

2 minutes read.

Reserved Keywords in Java

In Java, a reserved term is used as a code key called a keyword. Because they are predefined, these terms cannot be used for anything else. They cannot serve as...

3 minutes read.

Java Boolean booleanValue() method

The booleanValue() method of Java Boolean class returns a Boolean value for the specified Boolean argument. Syntax public Boolean booleanValue() Parameters NA Return Value This method returns the primitive value of specified Boolean object. Example 1 public class...

2 minutes read.

Java Open File

Java Desktop class give an open() strategy to open a filr. It has a place with a java.awt package. Work area execution is stage subordinate, so it is important to...

5 minutes read.

How to check Date is Greater in Java?

In this article, you will be acknowledged about how to check if the given date is greater than the other date or not. We are simply comparing the dates and...

7 minutes read.