String matches() Method in Java with Examples
Last Updated :
07 Aug, 2023
Variants of matches() method is used to tell more precisely not test whether the given string matches to a regular expression or not as whenever this method is called in itself as matches() or be it matches() where here we do pass two arguments that are our string and regular expression, the working and output remains same.
Variants of String matches() Method
There exist multiple variants three variants of the matches() method, as listed and described below, as follows:
- String matches() Method
- String regionMatches()
- String regionMatches() with ignoreCase
1. String matches() Method
This method tells whether or not this string matches the given regular expression. An invocation of this method of the form str.matches(regex) yields exactly the same result as the expression Pattern.matches(regex, str).
Syntax of String matches() method
public boolean matches (String regex)
Parameters
- regex: The regular expression to which this string is to be matched.
Return Type
- Boolean value, returning true if and only if strings match the given regular expression else false.
Example of String matches() Method
Java
public class GFG {
public static void main(String args[])
{
String Str = new String( "Welcome to geeksforgeeks" );
System.out.print(
"Does String contains regex (.*)geeks(.*) ? : " );
System.out.println(Str.matches( "(.*)geeks(.*)" ));
System.out.print(
"Does String contains regex geeks ? : " );
System.out.println(Str.matches( "geeks" ));
}
}
|
Output
Does String contains regex (.*)geeks(.*) ? : true
Does String contains regex geeks ? : false
2. String regionMatches() Method
This method has two variants that can be used to test if two string regions are equal.
Syntax of regionMatches() Method
public boolean regionMatches(int str_strt, String other, int other_strt,int len)
Parameters
- The starting offset of the subregion in this string
- The string argument
- The starting offset of the subregion in the string argument
- The number of characters to compare
Return Type
Boolean value, true if the specified subregion of this string matches the specified subregion of the string argument; false otherwise.
Example of regionMatches() Method
Java
public class GFG {
public static void main(String args[])
{
String Str1
= new String( "Welcome to geeksforgeeks" );
String Str2 = new String( "GEEKS" );
System.out.print(
"Checking if GEEKS is in geeksforgeeks( case sensitive ) : " );
System.out.println(
Str1.regionMatches( 11 , Str2, 0 , 5 ));
}
}
|
Output
Checking if GEEKS is in geeksforgeeks( case sensitive ) : false
3. String regionMatches() with ignoreCase
This method has two variants that can be used to test if two string regions are equal.
Syntax of String regionMatches()
public boolean
regionMatches(boolean ignoreCase, int str_strt, String other, int other_strt,int len)
Parameters
- The starting offset of the subregion in this string
- The string argument
- The starting offset of the subregion in the string argument
- The number of characters to compare
- ignoreCase: If true, ignore the case when comparing characters
Return Type
It returns true if the specified subregion of this string matches the specified subregion of the string argument; false otherwise. Whether the matching is exact or case insensitive depends on the ignoreCase argument.
Example of regionMatches() with ignoreCase
Java
public class GFG {
public static void main(String args[])
{
String Str1
= new String( "Welcome to geeksforgeeks" );
String Str2 = new String( "GEEKS" );
System.out.print(
"Checking if GEEKS is in geeksforgeeks( case insensitive ) : " );
System.out.println(
Str1.regionMatches( true , 11 , Str2, 0 , 5 ));
}
}
|
Output
Checking if GEEKS is in geeksforgeeks( case insensitive ) : true
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...